以下代码块仅在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)
答案 0 :(得分:0)
需要删除所有的message()调用。