Parallelize R code using snowfall package

250 views Asked by At

I am trying to run my R code in parallel. Following is the toy example in which myfunc function returns a number.

library(snowfall); 
sfInit(parallel=TRUE,cpus=5)
a <- 1 : 10000
sfExport("a")
parwrapper <- function(i){
        mysimulation <- myfunc(b=30,c=a[i])
        return(mysimulation)}
sfapply(1:10000,parwrapper)

This is the error that I get. Error in checkForRemoteErrors(val) : 5 nodes produced errors; first error: could not find function "myfunc"

1

There are 1 answers

8
Oliver On

Welcome to SO.

The error clearly states the problem. parwrapper calls a function myfunc. This function is not defined. In addition you might have to export the object sfExport('myfunc', 'parwrapper').