## The R worker processes in a cluster are fresh R processes that do ## not know about any non-default packages you have loaded or ## variables you have defined on the master. If you want to sue such ## packages or variables on your workers you need to make sure they ## are available. ## ## For variables, including functions, you can use clusterExport. A ## simple example: cl <- makeCluster(2) f1 <- function() 1 f2 <- function() 2 cl <- makeCluster(2) clusterExport(cl, c("f1", "f2")) parLapply(cl, 1:length(cl), function(i) i + f1() + f2()) ## For packages you can use clusterEvalQ to evaluate library() calls ## on each of the workers. For example, to load the mgcp package on ## all workers use clusterEvalQ(cl, library(mgcv)) ## You can also transfer data or functions as extra arguments to your ## work function, as done here: m <- function(x) sin(6 * x) gamsim <- function(n, m, s) { x <- seq(0, 1, length.out = n) y <- rnorm(m(x), s) gam(y ~ s(x)) } parLapply(cl, rep(50, length(cl)), gamsim, m, 0.1) ## Don't forget to shut down the cluster. stopCluster(cl)