从`parallel`包中的非基本R包中调用函数,而无需在函数中进行库化 [英] Calling functions from non-base R packages in `parallel` package without librarying them within the function

查看:97
本文介绍了从`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

这篇关于从`parallel`包中的非基本R包中调用函数,而无需在函数中进行库化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆