I had a problem getting some code to work with a parallel package in R. I am using R 2.15.
Here is a simplified example ... I have a file "animal.R" that contains the following:
setClass("Animal", representation(species = "character", legs = "numeric"))
setGeneric("count",function(x) standardGeneric("count"))
setMethod("count", "Animal", function(x) { x@legs})
setGeneric("countAfterChopping",function(x) standardGeneric("countAfterChopping"))
setMethod("countAfterChopping", "Animal", function(x) { x@legs <- x@legs-1; x@legs})
Then in my R-terminal, I run:
library(parallel)
source('animal.R')
Launch a local cluster of two nodes:
cl <- makeCluster(rep('localhost', 2))
Tell the cluster nodes about the Animal class:
clusterEvalQ(cl, parse('animal.R'))
Then run some code in the cluster:
parSapply(cl, list(daisy, fred), count)
parSapply(cl, list(daisy, fred), countAfterChopping)
Stop cluster:
stopCluster(cl)
The first call to parSapply works as expected, but the second causes this error:
Error in checkForRemoteErrors(val) :
2 nodes produced errors; first error: "Animal" is not a defined class
Any idea what is going on? Why does the second call to the parSapply function not work?