r - 带有自定义库的 package.install 无法加载包
问题描述
我正在尝试将具有依赖关系的 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 容器中)显示,安装运行良好,但加载包失败:
输出缩短。全贴在这里
$ 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")
推荐阅读
- python - Plotly Express scatter_mapbox 不在 Dash 中呈现
- mongodb - 连接到主机时出错:无法连接到服务器:服务器选择错误:服务器选择超时当前拓扑:类型:单服务器
- c++ - 如何将数据包缓冲区解析为结构
- git - Git 使用树形哈希将先前的提交恢复为新的提交
- java - 在 Java 中,为什么我可以将 double 添加到 int 但不能将 double 分配给 int?
- hyperledger-fabric - Hyperledger Fabric 资产/数据可以是十进制值还是只有整数?
- c# - 将英国格式的字符串 DateTime ("dd/MM/yyyy") 转换为文化可能发生变化的有效 DateTime
- amazon-web-services - Fn::GetOptionSetting 在 AWS ElasticBeanstalk 中不起作用
- c# - RABBITMQ C# 在控制台上运行良好,但不适用于服务
- c - 函数“池”的隐式声明在 C99 中无效