首页 > 解决方案 > 在 .Rprofile 中使用 default() 的问题

问题描述

我在 R 中使用的大多数库都是通过操作系统安装的(即使用apt install r-cran-*安装在 中的库/usr/lib/R/*)。不幸的是,当update.libraries()在会话中使用更新这些包时,我试图让 R 只在特定位置安装库。结果,我尝试以以下方式修改我的 .Rprofile :

## only update libraries installed in R sessions.
## Don't update libraries installed using apt.
## these libraries reside in /usr/lib/R
## Code based on: https://coolbutuseless.github.io/2018/04/11/changing-the-default-arguments-to-a-function/
library(default)
## See my note on stackexchange how this doesn't work as expected
## default(update.packages) <- list(lib.loc=grep("/usr/lib/R", .libPaths(), invert=TRUE, value=TRUE))
tmpUser = Sys.getenv("USER")
if( tmpUser =="root"){
    tmpLibPath <- "/usr/local/lib/R/site-library"
}else{
    tmpLibPath <- Sys.getenv("R_LIBS_USER")
}

default(update.packages) <- list(lib.loc = tmpLibPath)
default(install.packages) <- list(lib = tmpLibPath)
## Clean up
rm(c("tmpUser", "tmpLibPath")

但是,当我开始一个 R 会话时,我得到了错误,

`默认错误(update.packages)<-list(lib.loc = tmpLibPath):找不到对象'update.packages'

并且我的更改不会坚持(尽管如果我在交互式会话中执行代码,他们会坚持)。谁能帮助我(a)了解我做错了什么或(b)提出另一种实现这一目标的方法?

标签: rstartuprprofile

解决方案


推荐阅读