HappyCoding HappyCoding - 1 year ago 95
R Question

How to return null for custom function used in %dopar%?

I want to return NULL with a custom function used in a parallel

foreach
loop. The usage is that, if some result does not fulfill certain criteria, that result won't get stored and it'll get filtered out as a postprocess step.

However, my program returns


task 1 failed - "replacement has length zero"


The problem can be reproduced with following code:

library(doParallel)
Input <- c(F,T,F)
f.parallel <- function(x){
ifelse(x,T,NULL)
}
no_cores <- detectCores()-1
# for mac OS, linux
c1 <- makeCluster(no_cores,type = "FORK")
# for windows
# cl <- makeCluster(no_cores)
# clusterExport(cl, list("Input","f.parallel"))
registerDoParallel(cl)
output <- foreach(i=1:length(Input),.combine = cbind) %dopar% f.parallel(Input[i])

stopCluster(c1)

Answer Source

Try f.parallel <- function(x) if(x) TRUE. ifelse() won't allow you to return NULL.

When cbind() is called to combine the results of your loop, the NULL elements will be removed. If you'd like to filter them out in a post-process step, I'd recommend using ifelse(x, TRUE, NA).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download