使用自定义lib的package.install无法加载软件包

时间:2019-04-16 08:12:01

标签: r install.packages

我正在尝试将具有依赖性的R软件包安装到自定义库位置。这是最小的示例脚本(req.R):

dir.create("r_packages")
install.packages("R.utils", lib="r_packages")
library("R.utils", character.only = TRUE, lib.loc="r_packages")

使用Rscript(例如,在r-base docker container中运行)显示,安装正在运行 fine ,但正在加载< / strong>软件包失败

输出缩短。完整粘贴为here

$ Rscript req.R
also installing the dependencies ‘R.oo’, ‘R.methodsS3’

[...]

* installing *source* package ‘R.methodsS3’ ...
[...]
* DONE (R.methodsS3)
* installing *source* package ‘R.oo’ ...
[...]
* DONE (R.oo)
* installing *source* package ‘R.utils’ ...
[...]
* DONE (R.utils)

The downloaded source packages are in
        ‘/tmp/RtmpU4nBhU/downloaded_packages’
Error: package ‘R.oo’ required by ‘R.utils’ could not be found
Execution halted

加载以依赖关系的相反顺序一个一个包工作正常

$ cat req.R
dir.create("r_packages")
#install.packages("R.utils", lib="r_packages")
library("R.methodsS3", character.only = TRUE, lib.loc="r_packages")
library("R.oo", character.only = TRUE, lib.loc="r_packages")
library("R.utils", character.only = TRUE, lib.loc="r_packages")


$ Rscript req.R
Warning message:
In dir.create("r_packages") : 'r_packages' already exists
R.methodsS3 v1.7.1 (2016-02-15) successfully loaded. See ?R.methodsS3 for help.
R.oo v1.22.0 (2018-04-21) successfully loaded. See ?R.oo for help.

Attaching package: ‘R.oo’

The following objects are masked from ‘package:methods’:

    getClasses, getMethods

The following objects are masked from ‘package:base’:

    attach, detach, gc, load, save

R.utils v2.8.0 successfully loaded. See ?R.utils for help.

Attaching package: ‘R.utils’

The following object is masked from ‘package:utils’:

    timestamp

The following objects are masked from ‘package:base’:

    cat, commandArgs, getOption, inherits, isOpen, parse, warnings

有人暗示正确的方向吗?我在做什么错了?

1 个答案:

答案 0 :(得分:0)

经过一些研究,我发现,使用.libPaths()设置标准库路径可以解决此问题。显然,library()函数不会将lib.loc参数传递给后续调用。

这是最终代码:

dir.create("r_packages")
.libPaths(c("r_packages"))
install.packages("R.utils", lib="r_packages")
library("R.utils", character.only = TRUE, lib.loc="r_packages")