在并行化的ddply

时间:2017-09-04 03:31:27

标签: r parallel-processing try-catch plyr

以下代码块仅在ddply非并行化时才有效;即ddply(..., .parallel = FALSE)。为什么.parallel=TRUE时它不起作用?我有一个计算要执行,需要并行化,ddply是完美的,但我似乎无法弄清楚如何使用ddply并行化包含tryCatch()语句的函数。似乎ddply忽略了代码在tryCatch()范围内的事实。

# tryCatch in ddply

library(plyr)
library(dplyr)
library(reshape2)
library(parallel)
library(doParallel)

theFunc <- function(df){
  m <- df$a
  m <- tryCatch(
    {if(m>1){
      # do something normal
      m+1
    }else{
      # do something that throws an error
      m+"mehwhatever"
    }
    },
    warning=function(war){
      message(war)
      m <- df$a
      return(m)
    },
    error=function(cond) {
      message(cond)
      m <- df$a
      return(m)
    },
    finally={
      print("Does this even work?")
      print(m)
      }
  )
  df$a <- m
  return(df)
}

df <- data.frame(a=1:10)

print(df)
nodes <- detectCores(logical = FALSE)
cl <- makeCluster(nodes)
registerDoParallel(cl)

df <- ddply(.data = df,.variables = c("a"),.fun = function(x){return(theFunc(x))},.parallel = TRUE,.paropts = list(.export=c(as.vector(lsf.str()))))

parallel::stopCluster(cl)
print(df)

1 个答案:

答案 0 :(得分:0)

需要删除所有的message()调用。