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

查看:490
本文介绍了在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") )

或者(这将使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.

可以在?Startup帮助页面上阅读有关R启动过程的信息,并在以下位置找到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天全站免登陆