我正在尝试将具有依赖项的R包安装到自定义lib位置。这是最小的示例脚本(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中)显示,安装运行正常,但加载包失败:
输出缩短了。完全粘贴是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
任何暗示正确方向的人?我究竟做错了什么?
经过一些研究后我发现,用.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")