【发布时间】:2017-09-04 03:31:27
【问题描述】:
以下代码块仅在 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)
【问题讨论】:
-
是的 - 这是重复的。对不起!我环顾了很长时间,并没有遇到那个 - 很多道歉。我应该删除这个问题吗?
-
不,我想你可以离开它。有时使用不同的方法来搜索问题很有用。如果他们遇到您的问题,他们只会被重定向。
标签: r parallel-processing try-catch plyr