دنبال کننده ها

۱۳۹۶ شهریور ۱۲, یکشنبه

r - tryCatch() does NOT work when used within parallelized ddply

[ad_1]



The following chunk of code works only when ddply is non-parallelized; i.e., ddply(..., .parallel = FALSE). Why doesn't it work when .parallel=TRUE? I have a calculation to perform that requires parallelization and ddply is perfect for it, but I can't seem to figure out how to parallelize a function that includes a tryCatch() statement using ddply. It is as if ddply disregards the fact that the code is within a 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)



[ad_2]

لینک منبع