Home > Software design >  R parSapply does not recognize the function
R parSapply does not recognize the function

Time:10-28

I want to parallelize a user function using parSapply. I followed some tutorials and I figured it out the below code. However, the parSapply function returns an error:

library(parallel)

dummy_function <- function(x,y){
  x_ <- runif(1000, 0, x)
  y_ <- runif(1000, 0, y)
  return(median(x_*y_))
}

#it works with replicate
replicate(1000, dummy_function(2,5))


cl <- makeCluster(detectCores()-1)
clusterEvalQ(cl,library(MASS))
clusterSetRNGStream(cl)
clusterExport(cl, c("dummy_function"))

#it does not work with parSappply
results <- parSapply(cl, 1:1000, dummy_function(2,5))

# Error in match.fun(FUN) : 
#   'dummy_function(2, 5)' is not a function, character or symbol

stopCluster(cl)

Thank you!

CodePudding user response:

Create a lamdba function

results <- parSapply(cl, 1:1000, function(u) dummy_function(x = 2, y = 5))

-checking

> length(results)
[1] 1000

CodePudding user response:

Your issue: correct syntax is sapply(vec, fun) and not sapply(vec, fun(arg1, arg2).

So, given your set up, I would move the 2 and 5 as default values for the arguments of dummy_function:

dummy_function <- function(x=2, y=5) { # <-- changes made here
    x_ <- runif(1000, 0, x)
    y_ <- runif(1000, 0, y)
    return(median(x_*y_))
}

Then you can run replicate without passing the args:

replicate(1000, dummy_function())

And then you run parSapply (similarly without args):

results <- parSapply(cl, 1:1000, dummy_function)
  • Related