1
使用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
感谢,
最大
此解决方案似乎不再适用于在Mac OS X 10.10.5上运行的R 3.2.2(2015-08-14)。 –
@CraigW当你执行'x < - mclapply(1:3,message,mc.cores = 3)'时你会看到输出吗?你是否从“终端”执行标准R解释器? –
我收回。似乎在从终端运行R时显示输出,但不能从R.app GUI或RStudio运行。有什么办法可以让这个解决方案在这些环境中工作吗? –