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運行。有什麼辦法可以讓這個解決方案在這些環境中工作嗎? –