In a function, I need to run mclapply per each item in a list and it should also use a semi-global variable var.1. I don’t want to add var.1 to every list-item as it would take too much memory. Here is code that illustrate the problem:
library(parallel)
list.1 <- list(1,2,3,4)
myInnerFunction <- function(xx) {
return(xx+var.1)
}
myOuterFunction <- function(list.x) {
var.1 <- 17
tmp.1 <- mclapply(list.x, myInnerFunction, mc.cores=6)
return(tmp.1)
}
results <- myOuterFunction(list.x=list.1)
[1] "Error in FUN(X[[1L]], ...) : object 'var.1' not found\n"
results[[1]] # This should be 18
How can I pass var.1 to mclapply? var.1 must be declared inside myOuterFunction.
You could create a second (
var.1) argument for the functionmyInnerFunction:Now it is possible to pass the second argument for the
myInnerFunctionfunction in themclapplycommand:The result: