1 Including fixed objects

R is fun language for computer programming and statistics, but it’s not without it’s quirks. For instance, R generally has a recursive strategy when attempting to find objects within functions. If an object can’t be found, R will start to look outside the function’s environment to see if the object can be located there, and if not, look within even higher-level environments… This recursive search continues until it searches for the object in the user workspace/Global environment, and only when the object can’t be found here will an error be thrown. This is a strange feature to most programmers who come from other languages, and when writing simulations may cause some severely unwanted issues. This tutorial demonstrates how to make sure all required user-defined objects are visible to SimDesign.

2 Scoping

To demonstrate the issue, let’s define two objects and a function which uses these objects.

obj1 <- 10
obj2 <- 20

When evaluated, these objects are visible to the user, and can be seen by typing in the R console by typing ls(). Functions which do not define objects with the same name will also be able to locate these values.

myfun <- function(x) obj1 + obj2
## [1] 30

This behavior is indeed a bit strange, but it’s one of R’s quirks. Unfortunately, when running code in parallel across different cores these objects will not be visible, and therefore must be exported using other methods (e.g., in the parallel package this is done with clusterExport()).

cl <- makeCluster(2)
res <- try(parSapply(cl=cl, 1:4, myfun))
## Error in checkForRemoteErrors(val) : 
##   2 nodes produced errors; first error: object 'obj1' not found

Exporting the objects to the cluster fixes the issue.

clusterExport(cl=cl, c('obj1', 'obj2'))
parSapply(cl=cl, 1:4, myfun)
## [1] 30 30 30 30

The same reasoning above applies to functions defined in th