在 Rprofile.site 中使用 .libPaths 更改 R 默认库路径无法正常工作 [英] Change R default library path using .libPaths in Rprofile.site fails to work

查看:31
本文介绍了在 Rprofile.site 中使用 .libPaths 更改 R 默认库路径无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Windows 上运行 R,而不是作为管理员.当我安装一个包时,以下命令不起作用:

I am running R on Windows, not as an administrator. When I install a package, the following command doesn't work:

> install.packages("zoo")
Installing package(s) into ‘C:/Program Files/R/R-2.15.2/library’
(as ‘lib’ is unspecified)
Warning in install.packages :
  'lib = "C:/Program Files/R/R-2.15.2/library"' is not writable

要安装一个包,我必须指定一个库位置:

To install a package, I have to specify a library location:

install.packages("zoo", lib="C:/software/Rpackages")

要加载一个包,我还必须指定库位置:

To load a package, I also have to specify the library location:

library("zoo", lib.loc="C:/software/Rpackages")

所有这些都可以,但我想看看是否可以以某种方式将 C:/software/Rpackages 添加到库路径中,因此不必每次都键入它.

All of this is OK, but I wanted to see if I could add C:/software/Rpackages to the library path somehow and thus not have to type it each time.

当我在网上搜索时,我发现一种方法是编辑 Rprofile.site 文件并添加该行

As I searched online, I found that one way to do this is to edit the Rprofile.site file and to add the line

.libPaths("C:/software/Rpackages")

但是,在这样做并启动 RStudio 之后,这是我得到的输出

However, after doing this, and starting RStudio, this is the output that I get

> .libPaths()
[1] "C:/Program Files/R/R-2.15.2/library" "C:/Program Files/RStudio/R/library" 

我添加到 Rprofile.site.libPaths 命令似乎没有任何效果!为什么会这样?或者更重要的是,我该如何解决这个问题,以便在不输入库位置的情况下安装和加载包?

The .libPaths command that I added to the Rprofile.site doesn't seem to have had any effect! Why is this the case? Or more importantly, how can I fix the problem so that I can install and load packages without typing in the library location?

注意:如果我启动 RStudio,.libPaths() 命令似乎可以正常工作

Note: if I start RStudio the .libPaths() command seems to work as it is supposed to

.libPaths("C:/software/Rpackages")
> .libPaths()
[1] "C:/software/Rpackages"               "C:/Program Files/R/R-2.15.2/library"

这不是很奇怪吗?

推荐答案

我通常会尝试将我所有的包保存在一个库中,但是如果你想添加一个库为什么不附加新的库(它必须已经存在于你的文件系统)到现有的库路径?

I generally try to keep all of my packages in one library, but if you want to add a library why not append the new library (which must already exist in your filesystem) to the existing library path?

.libPaths( c( .libPaths(), "~/userLibrary") )
 # obviously this would need to be a valid file directory in your OS
 # min just happened to be on a Mac that day

或者(这将使 userLibrary 成为放置新包的第一个位置):

Or (and this will make the userLibrary the first place to put new packages):

.libPaths( c( "~/userLibrary" , .libPaths() ) )

然后我得到(至少在我最初写这篇文章时):

Then I get (at least back when I wrote this originally):

> .libPaths()
[1] "/Library/Frameworks/R.framework/Versions/2.15/Resources/library"
[2] "/Users/user_name/userLibrary"  

.libPaths 函数与大多数其他非图形函数有点不同.它通过副作用起作用.报告和更改 R 环境变量的函数 Sys.getenvSys.setenv 已分开,但 .libPaths 可以报告或更改它的目标.

The .libPaths function is a bit different than most other nongraphics functions. It works via side-effect. The functions Sys.getenv and Sys.setenv that report and alter the R environment variables have been split apart but .libPaths can either report or alter its target.

关于 R 启动过程的信息可以在 ?Startup 帮助页面中阅读,RStudio 材料位于:https://support.rstudio.com/hc/en-us/articles/200549016-Customizing-RStudio

The information about the R startup process can be read at ?Startup help page and there is RStudio material at: https://support.rstudio.com/hc/en-us/articles/200549016-Customizing-RStudio

在您的情况下,RStudio 似乎不尊重 Rprofile.site 设置,或者可能通过从 RStudio 默认值之一读取 .Rprofile 设置来覆盖它们.还应该提到的是,此操作的结果还附加了对 .Library.Library.site 的调用内容,这也是为什么 RStudio-(或任何其他 IDE 或网络安装-) 托管的 R 可能会表现出不同的行为.

In your case it appears that RStudio is not respecting the Rprofile.site settings or perhaps is overriding them by reading an .Rprofile setting from one of the RStudio defaults. It should also be mentioned that the result from this operation also appends the contents of calls to .Library and .Library.site, which is further reason why an RStudio- (or any other IDE or network installed-) hosted R might exhibit different behavior.

由于Sys.getenv()返回R进程的当前系统环境,你可以看到库和其他路径:

Since Sys.getenv() returns the current system environment for the R process, you can see the library and other paths with:

Sys.getenv()[ grep("LIB|PATH", names(Sys.getenv())) ]

存储和访问包的两个重要因素是(现在在 Linux 机器上有所不同):

The two that matter for storing and accessing packages are (now different on a Linux box):

R_LIBS_SITE                          /usr/local/lib/R/site-library:/usr/lib/R/site-library:/usr/lib/R/library
R_LIBS_USER                          /home/david/R/x86_64-pc-linux-gnu-library/3.5.1/
 

这篇关于在 Rprofile.site 中使用 .libPaths 更改 R 默认库路径无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆