从`parallel`包中的非基本R包中调用函数,而无需在函数中进行库化
问题描述:
让我们说我正在尝试运行以下代码
Lets say I'm trying to run the following code
library(gregmisc)
library(parallel)
myfunction <- function(x){
combinations(10, x, 1:10)
}
cl <- makeCluster(getOption("cl.cores", 2))
parLapply(cl, 3, myfunction)
我遇到错误
#Error in checkForRemoteErrors(val) :
#one node produced an error: could not find function "combinations"
因此,如果我将"gregmisc"包存储在该函数中,它将起作用
So if I'll library "gregmisc" package within the function it will work
myfunction <- function(x){
library(gregmisc)
combinations(10, x, 1:10)
}
cl <- makeCluster(getOption("cl.cores", 2))
parLapply(cl, 3, myfunction)
问题是,如何避免在函数中使用库软件包?
我看到此处
但是我无法在并行"程序包中使用它
but I couldn't get it to work for the "parallel" package
我已经尝试过(没有成功)
I've tried (without success)
library(snow)
library(snowfall)
sfExport(list=list("combinations"))
sfLibrary(gregmisc)
clusterEvalQ(cl, library(gregmisc))
答
我在 gregmisc 中看不到任何combinations
函数.这可能是您的实际问题吗?
I don't see any combinations
function in gregmisc. Could that be your actual problem?
在每个节点上使用clusterEvalQ()
加载软件包应该可以,并且一直对我有用.
以下代码从vignette("parallel")
的第8页几乎逐字删除:
Loading packages on each node with clusterEvalQ()
should work, and always has worked for me.
The following code is lifted nearly verbatim from page 8 of vignette("parallel")
:
require(parallel)
cl <- makeCluster(4)
junk <- clusterEvalQ(cl, library(boot)) ## Discard result