Search code examples
rmclapply

Semi-global variable to mclapply


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.


Solution

  • You could create a second (var.1) argument for the function myInnerFunction:

    myInnerFunction <- function(xx, var.1) {
      return(xx+var.1)
    }
    

    Now it is possible to pass the second argument for the myInnerFunction function in the mclapply command:

    myOuterFunction <- function(list.x) {
      var.1 <- 17
      tmp.1 <- mclapply(list.x, myInnerFunction, var.1, mc.cores=6)
      return(tmp.1)
    }
    

    The result:

    results <- myOuterFunction(list.x=list.1)
    
    
    [[1]]
    [1] 18
    
    [[2]]
    [1] 19
    
    [[3]]
    [1] 20
    
    [[4]]
    [1] 21