我试图parLapply在全局环境中未定义的另一个函数内部使用.worker函数使用我想要的其他函数列表,这些函数clusterExport也未在全局环境中定义.我的问题是两个函数都将它们的评估环境导出到集群,这些集群很庞大而且不需要.
让我们调用worker函数workerFunction和函数列表functionList.
workerFunction <- function(i) {
intermediateOutput <- functionList[[i]](y)
result <- otherCalculations(intermediateOutput)
return(result)
}
library(parallel)
cl <- makeCluster(detectCores())
environment(workerFunction) <- .GlobalEnv
environment(functionList) <- .GlobalEnv
clusterExport(cl, varlist=c("functionList", "y"), envir=.GlobalEnv)
output <- parLapply(cl, inputVector, workerFunction)
Run Code Online (Sandbox Code Playgroud)
我明白了:
Error in get(name, envir = envir) (from <text>#53) : object 'functionList' not found
Run Code Online (Sandbox Code Playgroud)
如果我没有设置environment(functionList) <- .GlobalEnv,则将巨大的封闭环境functionList导出到集群.为什么R不能functionList在全球环境中找到?