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

> 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

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

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

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

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

所有这些都没问题,但我想看看是否可以将C:/software/Rpackages添加到库路径中,这样就不必每次都输入它了。

当我在网上搜索时,我发现这样做的一种方法是编辑Rprofile。站点文件并添加行

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

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

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

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

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

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

这不是很奇怪吗?


当前回答

我在这里找到了我认为的解决方案(感谢SFU的Carl Schwarz),您添加了一个个人库,无论使用R还是Rstudio,它都是永久识别的(您不必每次会话都定义它),Rstudio将它作为我Mac机器上的默认值。我没有在SO上看到它明确地列出这一点,所以我总结了他们提供的步骤,先是Windows,然后是Mac。

Windows 7操作系统:

Create a directory on the drive where you want to have your personal library, e.g. C:\User\Rlibs (or another that you have permissions to) Search for/go to "Edit environment variable for your account" in the Windows search bar to edit control panel settings Click "New..." in the middle of the "Environmental Variables" window In the "New User Variable" window, type R_LIBS for the "Variable name", and the path to the personal library directory you created, e.g. C:\User\Rlibs Click OK and you should see the Variable/Value pair in the User variables window Click OK again

现在,当您启动R(或Rstudio)并键入. libpaths()命令时,您应该会看到您创建的个人库以及R系统库。

Mac:

在“Home”或“username”目录中创建一个名为Rlibs的文件夹 启动终端应用程序 类型:echo "R_LIBS=~/Rlibs" > . renviron确保拼写和大小写匹配。 输入ls -a查看目录中文件的完整列表,现在应该包括.Renvrion 验证.Renviron文件已正确设置:更多.Renviron

启动R/Rstudio并输入. libpaths(),您应该会看到个人库的新路径。

其他回答

我真的很难理解。当我上次重新安装OS & Rstudio时,gorkypl给出了正确的解决方案,但这一次,设置我的环境变量没有解决。

卸载R和Rstudio,创建目录C:\R和C:\Rstudio,然后重新安装它们。

将R_LIBS_USER用户变量定义到您的首选目录(根据gorkypl的回答),并重新启动您的机器以加载user变量。打开Rstudio,错误应该消失了。

您还可以使用Sys.setenv()将R_LIBS_USER修改为您的替代库的路径,这更容易,而且不需要重新启动您的计算机。

要查看R_LIBS_USER设置为什么: ? Sys.getenv ()

阅读帮助(启动)是有用的。

在Ubuntu上,为用户更改默认库路径的推荐方法是在~/. conf中设置R_LIBS_USER变量。Renviron文件。

touch ~/.Renviron
echo "R_LIBS_USER=/custom/path/in/absolute/form" >> ~/.Renviron

https://superuser.com/questions/749283/change-rstudio-library-path-at-home-directory

编辑~/。伦维隆

R_LIBS_USER=/some/path

由于这里的大多数答案都与Windows和Mac OS有关,(考虑到我也在努力解决这个问题)我决定在Arch Linux安装中发布帮助我解决这个问题的过程。

步骤1:

Do a global search of your system (e.g. ANGRYSearch) for the term Renviron (which is the configuration file where the settings for the user libraries are set). It should return only two results at the following directory paths: /etc/R/ /usr/lib/R/etc/ NOTE: The Renviron config files stored at 1 & 2 (above) are hot-linked to each other (which means changes made to one file will automatically be applied [ in the same form / structure ] to the other file when the file being edited is saved - [ you also need sudo rights for saving the file post-edit ] ).

步骤2:

导航到第一个目录路径(/etc/R/),并用您最喜欢的文本编辑器打开Renviron文件。 进入Renviron文件后,搜索R_LIBS_USER标记,并将花括号部分中的文本更新为所需的目录路径。 例子: ... 更改来源(原始条目): R_LIBS_USER = $ {R_LIBS_USER - R ~ / / x86_64-pc-linux-gnu-library / 4.0 '} ... 更改到(您想要的条目): R_LIBS_USER = $ {R_LIBS_USER——“~ /应用程序/ R / rUserLibs '}

步骤3:

保存你刚刚编辑的Renviron文件……完成了! !

只需将R库的默认文件夹更改为没有管理员权限的目录,例如:

.libPaths("C:/R/libs")