如何删除打印抑制 - 并行包

时间:2016-06-21 18:38:49

标签: r parallel-processing

parallel包会使用parSapplyparLapply来抑制不返回的打印。是否有可能绕过这种影响?

正常(预期行为)

sapply(iris, function(x) {
  print("x")
  message("message")
  warning("warning")
  return(x[2])
})
# [1] "x"
# message
# [1] "x"
# message
# [1] "x"
# message
# [1] "x"
# message
# [1] "x"
# message
# Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
#          4.9          3.0          1.4          0.2          1.0 
# Warning messages:
# 1: In FUN(X[[i]], ...) : warning
# 2: In FUN(X[[i]], ...) : warning
# 3: In FUN(X[[i]], ...) : warning
# 4: In FUN(X[[i]], ...) : warning
# 5: In FUN(X[[i]], ...) : warning

并行处理(意外抑制)

library(parallel)
cl <- makeCluster(3)
parSapply(cl, iris, function(x) {
  print("x")
  message("message")
  warning("warning")
  return(x[2])
})
# Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
#          4.9          3.0          1.4          0.2          1.0

#Run after
stopCluster(cl)

1 个答案:

答案 0 :(得分:0)

由于issues with parallel access to R terminal from separate processes forked by parallel

,我不确定这是否可行

如果您需要从流程中获取消息,请this SO answer should help(简要说明:在outfile中使用parallel::makeCluster param)