在foreach中指定包的位置 [英] Specify package location in foreach

查看:163
本文介绍了在foreach中指定包的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将软件包安装到特定目录,然后使用以下命令加载到库中:

I am installing a package to a particular directory and then loading in the library using:

library(CustomPackage, lib.loc = "R_Libs")

然后在使用foreach时,我很难弄清楚如何从该自定义位置"R_Libs"加载此软件包.

Then when using foreach I am having trouble figuring out how to load this one package from that custom location "R_Libs".

foreach(i=(1:100), .packages=c("lubridate","CustomPackage")) %dopar% {
some code here...
}

有什么想法如何强制从"R_Libs"目录中读取一个软件包?

Any ideas how to force that one package to be read from the "R_Libs" directory?

推荐答案

在R控制台中修改库路径是没有意义的.

Modifying library paths in R console is meaningless.

> library(doParallel)
> library(foreach)
> cl = makeCluster(detectCores() - 1)
> registerDoParallel(cl)
> getDoParWorkers()
[1] 3
> .libPaths()
[1] "D:/Program Files/R/R-3.2.3/library"
> .libPaths(c(.libPaths(), "C:/"))
> .libPaths()
[1] "D:/Program Files/R/R-3.2.3/library" "C:/"   

在foreach内部,库路径仍然是默认路径:

Inside foreach, the library paths are still the default:

> tmp = foreach(j = 1:2) %dopar% {.libPaths()}
> tmp
[[1]]
[1] "D:/Program Files/R/R-3.2.3/library"

[[2]]
[1] "D:/Program Files/R/R-3.2.3/library"

虽然我不确定foreach的工作原理,但是它的想法是启动几个新的Rscripts.在每个新的Rscript中,库路径将是Rprofile.site中指定的默认路径.

Though I'm not sure how exactly foreach works, but the idea is to start several new Rscripts. In each new Rscript, library paths will be the default ones specified in Rprofile.site.

所以最方便的方法是在Rprofile.site中的D:\ Program Files \ R \ R-3.2.3 \ etc \

So the most convenient way is to add paths in Rprofile.site under D:\Program Files\R\R-3.2.3\etc\

另一种方法是手动加载库,即

Another way is to load the library by hand, i.e.

tmp = foreach(j = 1:2) %dopar% {
          library(xxx, lib.loc = /xxx/xx)
          ...
      }

这更加灵活,尤其是当人们无法访问Rprofile.site时.

This is more flexible, especially when one cannot access Rprofile.site.

这篇关于在foreach中指定包的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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