我正在通过群集分配作业,而我宁愿不去每台机器并手动安装正确的软件包。作业控制器以无人的身份运行脚本,因此我必须为安装指定无争议的可写路径。我实际上有这个有效的解决方案:
`%ni%` = Negate(`%in%`) ### "not in"
.libPaths("/tmp/") ### for local (remote non super user) install of packages
if ("xxx" %ni% installed.packages()) {install.packages("xxx", repos = "http://cran.r-project.org", lib="/tmp/")}
# ... and more
library(xxx)
# ... and more
起初工作,但一周后我遇到了一个奇怪的问题。
> library(xxx)
Error in library(xxx) : there is no package called 'xxx'
两个问题: 我应该采用不同的方式管理远程安装吗? 是什么原因导致我的问题是加载包失败?
请免除我必须实施像
这样的kludgelocdir <- paste("/tmp/", as.integer(runif(1, 1, 100000)), sep='')
system(paste("mkdir", locdir))
.libPaths(locdir)
install.packages("xxx", repos = "http://cran.r-project.org", lib=locdir)
library(xxx)
答案 0 :(得分:2)
您可能需要选项character.only = TRUE
,尽管您的代码之前的工作很奇怪但不再适用。无论如何,试试这个功能:
packageLoad<-function(libName){
# try to load the package
if (!require(libName,character.only = TRUE)){
# if package is not available, install it
install.packages(libName,dep=TRUE,
repos="http://cran.r-project.org",lib="/tmp/",destdir="/tmp/")
# try again
if(!require(libName,character.only = TRUE))
stop(paste("Package ",libName,"
not found and its installation failed."))
}
}