使用 foreach 和 doMC,有没有办法在并行运行时获取cat
和/或message
打印到屏幕?例如:
> library(foreach)
>
> tmp <- foreach(i=1:3) %do%
+ cat("sqrt(i) =", sqrt(i), "\n")
sqrt(i) = 1
sqrt(i) = 1.414214
sqrt(i) = 1.732051
>
> tmp <- foreach(i=1:3) %do%
+ message("i^2 =", i^2, "\n")
i^2 =1
i^2 =4
i^2 =9
>
> library(doMC)
> registerDoMC(2)
>
> tmp <- foreach(i=1:3) %dopar%
+ cat("sqrt(i) =", sqrt(i), "\n")
>
> tmp <- foreach(i=1:3) %dopar%
+ message("i^2 =", i^2, "\n")
>
> sessionInfo()
R Under development (unstable) (2014-01-29 r64898)
Platform: x86_64-apple-darwin10.8.0 (64-bit)
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] parallel stats graphics grDevices utils datasets
[7] methods base
other attached packages:
[1] doMC_1.3.2 iterators_1.0.6 foreach_1.4.1
loaded via a namespace (and not attached):
[1] codetools_0.2-8 compiler_3.1.0 tools_3.1.0
谢谢,
最大限度