简体   繁体   English

在 Rprofile.site 中使用 .libPaths 更改 R 默认库路径失败

[英]Change R default library path using .libPaths in Rprofile.site fails to work

I am running R on Windows, not as an administrator.我在 Windows 上运行 R,而不是作为管理员。 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")

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.所有这些都可以,但我想看看我是否可以以某种方式将C:/software/Rpackages到库路径中,从而不必每次都输入它。

As I searched online, I found that one way to do this is to edit the Rprofile.site file and to add the line当我在网上搜索时,我发现一种方法是编辑Rprofile.site文件并添加行

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

However, after doing this, and starting RStudio, this is the output that I get但是,在执行此操作并启动 RStudio 之后,这是我得到的输出

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

The .libPaths command that I added to the Rprofile.site doesn't seem to have had any effect!我添加到Rprofile.site.libPaths命令似乎没有任何效果! 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?或者更重要的是,如何解决问题,以便我可以在不输入库位置的情况下安装和加载包?

Note: if I start RStudio the .libPaths() command seems to work as it is supposed to注意:如果我启动 RStudio, .libPaths()命令似乎可以正常工作

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

Isn't that strange?这不奇怪吗?

The proper solution is to set environment variable R_LIBS_USER to the value of the file path to your desired library folder as opposed to getting RStudio to recognize a Rprofile.site file.正确的解决方案是将环境变量R_LIBS_USER设置为所需库文件夹的文件路径值,而不是让 RStudio 识别 Rprofile.site 文件。

To set environment variable R_LIBS_USER in Windows, go to the Control Panel (System Properties -> Advanced system properties -> Environment Variables -> User Variables) to a desired value (the path to your library folder), eg要在 Windows 中设置环境变量R_LIBS_USER ,请转到控制面板(系统属性 -> 高级系统属性 -> 环境变量 -> 用户变量)为所需值(库文件夹的路径),例如

Variable name: R_LIBS_USER 
Variable value: C:/software/Rpackages  

If for some reason you do not have access to the control panel, you can try running rundll32 sysdm.cpl,EditEnvironmentVariables from the command line on Windows and add the environment variable from there.如果由于某种原因您无法访问控制面板,您可以尝试从 Windows 的命令行运行rundll32 sysdm.cpl,EditEnvironmentVariables并从那里添加环境变量。

Setting R_LIBS_USER will ensure that the library shows up first in .libPaths() regardless of starting RStudio directly or by right-clicking an file and "Open With" to start RStudio.设置 R_LIBS_USER 将确保库首先出现在 .libPaths() 中,无论是直接启动 RStudio 还是右键单击文件并“打开方式”启动 RStudio。

The Rprofile solution can work if RStudio is always started by clicking the RStudio shortcut.如果始终通过单击 RStudio 快捷方式启动 RStudio,则 Rprofile 解决方案可以工作。 In this case,setting the default working directory to the directory that houses your Rprofile will be sufficient.在这种情况下,将默认工作目录设置为存放 Rprofile 的目录就足够了。 The Rprofile solution does not work when clicking on a file to start RStudio because that changes the working directory away from the default working directory.单击文件以启动 RStudio 时,Rprofile 解决方案不起作用,因为这会将工作目录从默认工作目录更改为其他目录。

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

Or (and this will make the userLibrary the first place to put new packages):或者(这将使 userLibrary 成为放置新包的第一个位置):

.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"  

The .libPaths function is a bit different than most other nongraphics functions. .libPaths函数与大多数其他非图形函数有点不同。 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 环境变量的Sys.getenvSys.setenv函数已被拆分,但.libPaths可以报​​告或更改其目标。

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

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

Since Sys.getenv() returns the current system environment for the R process, you can see the library and other paths with:由于Sys.getenv()返回 R 进程的当前系统环境,您可以通过以下方式查看库和其他路径:

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

The two that matter for storing and accessing packages are (now different on a Linux box):对存储和访问包很重要的两个是(现在在 Linux 机器上不同):

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/
 

I managed to solve the problem by placing the code in the .Rprofile file in the default working directory.我设法通过将代码放在默认工作目录中的.Rprofile文件中来解决这个问题。

First, I found the location of the default working directory首先,我找到了默认工作目录的位置

> getwd()
[1] "C:/Users/me/Documents"

Then I used a text editor to write a simple .Rprofile file with the following line in it然后我使用文本编辑器编写了一个简单的.Rprofile文件,其中包含以下行

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

Finally, when I start R and run .libPaths() I get the desired output:最后,当我启动R并运行.libPaths()我得到了所需的输出:

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

I found what I think is a solution here (thank you Carl Schwarz at SFU) for adding a personal library that is permanently (you don't have to define it each session) recognized whether using R or Rstudio, and Rstudio treats it as the default on my Mac machine.我在这里找到了我认为的解决方案(感谢 SFU 的 Carl Schwarz)添加一个永久(您不必在每个会话中定义它)识别使用 R 或 Rstudio 的个人库,并且 Rstudio 将其视为默认在我的 Mac 机器上。 I hadn't seen it laid out this explicitly on SO, so I summarized the steps they provided, for Windows and then for Mac.我没有看到它在 SO 上明确列出,所以我总结了他们为 Windows 和 Mac 提供的步骤。

For a Windows 7 OS:对于 Windows 7 操作系统:

  1. Create a directory on the drive where you want to have your personal library, eg C:\\User\\Rlibs (or another that you have permissions to)在您想要拥有个人库的驱动器上创建一个目录,例如 C:\\User\\Rlibs(或其他您有权访问的目录)

  2. Search for/go to "Edit environment variable for your account" in the Windows search bar to edit control panel settings在 Windows 搜索栏中搜索/转到“为您的帐户编辑环境变量”以编辑控制面板设置

  3. Click "New..." in the middle of the "Environmental Variables" window单击“环境变量”窗口中间的“新建...”

  4. In the "New User Variable" window, type R_LIBS for the "Variable name", and the path to the personal library directory you created, eg C:\\User\\Rlibs在“新建用户变量”窗口中,为“变量名称”键入R_LIBS ,以及您创建的个人库目录的路径,例如C:\\User\\Rlibs

  5. Click OK and you should see the Variable/Value pair in the User variables window单击“确定”,您应该会在“用户变量”窗口中看到“变量/值”对

  6. Click OK again再次点击确定

Now when you start R (or Rstudio) and type the command .libPaths() you should see the personal library you created as well as the R system library.现在,当您启动 R(或 Rstudio)并键入命令.libPaths()您应该会看到您创建的个人库以及 R 系统库。

For Mac:对于 Mac:

  1. In your "Home" or "username" directory create a folder called Rlibs在您的“主页”或“用户名”目录中创建一个名为Rlibs的文件夹

  2. Launch the Terminal application启动终端应用程序

  3. Type: echo "R_LIBS=~/Rlibs"> .Renvrion Make sure case matches.输入: echo "R_LIBS=~/Rlibs"> .Renvrion确保大小写匹配。

  4. Type ls -a to see the full list of files in the directory, which should now include .Renvrion键入ls -a以查看目录中文件的完整列表,其中现在应包含.Renvrion

  5. Verify that the .Renviron file has been set properly: more .Renviron验证.Renviron文件是否已正确设置: more .Renviron

Launch R/Rstudio and type .libPaths() and you should see the new path to your personal library.启动 R/Rstudio 并输入.libPaths() ,您应该会看到个人库的新路径。

If you do not have admin-rights, it can also be helpful to open the Rprofile.site -file located in \\R-3.1.0\\etc and add:如果您没有管理员权限,打开位于\\R-3.1.0\\etcRprofile.site文件并添加:

.First <- function(){
  .libPaths("your path here")
}

This evaluates the .libPath() command directly at start这会在开始时直接评估.libPath()命令

I've had real trouble understanding this.我真的很难理解这一点。 gorkypl gave the correct solution above when I last re-installed my OS & Rstudio but this time round, setting my environment variable didn't resolve.当我上次重新安装我的操作系统和 Rstudio 时,gorkypl 给出了正确的解决方案,但这一次,设置我的环境变量没有解决。

Uninstallled both R and Rstudio, creating directories C:\\R and C:\\Rstudio then reinstalled both.卸载 R 和 Rstudio,创建目录 C:\\R 和 C:\\Rstudio,然后重新安装。

Define R_LIBS_USER user variable to your prefered directory (as per gorkypl's answer) and restart your machine for User variable to be loaded.将 R_LIBS_USER 用户变量定义到您喜欢的目录(根据 gorkypl 的回答)并重新启动您的机器以加载用户变量。 Open Rstudio, errors should be gone.打开Rstudio,错误应该消失了。

You can also use Sys.setenv() to modify R_LIBS_USER to the path of your alternative library which is easier and does not need to restart your computer.您还可以使用Sys.setenv()将 R_LIBS_USER 修改为您的替代库的路径,这样更容易并且不需要重新启动计算机。

To see what R_LIBS_USER is set to: ?Sys.getenv()要查看 R_LIBS_USER 设置为: ?Sys.getenv()

Reading help(Startup) is useful.阅读help(Startup)很有用。

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

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

On Ubuntu, the recommended way of changing the default library path for a user, is to set the R_LIBS_USER variable in the ~/.Renviron file.在 Ubuntu 上,为用户更改默认库路径的推荐方法是在~/.Renviron文件中设置R_LIBS_USER变量。

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

If your default package library has been changed after installing a new version of R or by any other means, you can append both the libraries to use all the packages with the help of the commands below.如果在安装新版本的 R 后或通过任何其他方式更改了默认包库,您可以在以下命令的帮助下附加这两个库以使用所有包。 Get the existing library path :获取现有库路径:

.libPaths()

Now,set the existing and the old path :现在,设置现有路径和旧路径:

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

Hope it helps.希望能帮助到你。

I read the readme.我读了自述文件。 In that they mentioned use .libPaths() in command line to check which paths are there.他们提到在命令行中使用 .libPaths() 来检查存在哪些路径。 I had 2 library paths earlier.我之前有 2 个库路径。 When I used the command .libpath("C:/Program Files/R/R-3.2.4revised/library") where I wanted, it changed the library path.当我在我想要的地方使用命令 .libpath("C:/Program Files/R/R-3.2.4revised/library") 时,它改变了库路径。 When I typed in .libPaths() at the command line again it showed me the correct path.当我再次在命令行中输入 .libPaths() 时,它显示了正确的路径。 Hope this helps希望这可以帮助

getwd()
# [1] "C:/Users/..../software/My R studio"

copy the above link with double inverted comma用双引号复制上面的链接

.libPaths(new="C:/Users/..../software/My R studio")

Your default path will change for installing pakages您的默认路径将更改以安装包

If you want to change your library path permanently (without calling .libPath() every time when entering in R, this works for me:如果您想永久更改库路径(每次输入 R 时不调用 .libPath(),这对我有用:

  1. create .Rprofile under your home directory.在您的主目录下创建 .Rprofile。 (~/.Rprofile) (~/.Rprofile)

  2. type .libPaths(c( .libPaths(), "your new path" )) in .Rprofile file, save.在 .Rprofile 文件中输入.libPaths(c( .libPaths(), "your new path" )) ,保存。

  3. open R (any directory) and check, just type .libPaths() , you can find your libaray path is updated!打开R(任何目录)并检查,只需键入.libPaths() ,您就会发现您的libaray路径已更新!

Since most of the answers here are related to Windows & Mac OS, (and considering that I also struggled with this) I decided to post the process that helped me solve this problem on my Arch Linux setup.由于这里的大多数答案都与 Windows 和 Mac OS 相关(并且考虑到我也在为此而苦恼)我决定在我的Arch Linux设置上发布帮助我解决这个问题的过程。

Step 1:第1步:

  • Do a global search of your system (eg ANGRYSearch ) for the term Renviron (which is the configuration file where the settings for the user libraries are set).对您的系统进行全局搜索(例如ANGRYSearch )以查找术语Renviron (这是设置用户库设置的配置文件)。
  • It should return only two results at the following directory paths:它应该只返回以下目录路径中的两个结果:
    1. /etc/R/
    2. /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 ] ) .注意:存储在 1 和 2(上面)的Renviron配置文件彼此热链接(这意味着对一个文件所做的更改将在编辑文件时自动应用 [以相同的形式/结构] 到另一个文件)已保存 - [您还需要sudo权限来保存编辑后的文件])

Step 2:第2步:

  • Navigate into the 1st directory path ( /etc/R/ ) and open the Renviron file with your favourite text editor.导航到第一个目录路径 ( /etc/R/ ) 并使用您喜欢的文本编辑器打开Renviron文件。
  • Once inside the Renviron file search for the R_LIBS_USER tag and update the text in the curly braces section to your desired directory path.Renviron文件中搜索R_LIBS_USER标记并将花括号部分中的文本更新为所需的目录路径。

    EXAMPLE:例子:
    ... Change From ( original entry ): ...更改自原始条目):
    R_LIBS_USER=${R_LIBS_USER-'~/R/x86_64-pc-linux-gnu-library/4.0'}
    ... Change To ( your desired entry ): ...更改为您想要的条目):
    R_LIBS_USER=${R_LIBS_USER-'~/Apps/R/rUserLibs'}

Step 3:第 3 步:

  • Save the Renviron file you've just edited ... DONE !!保存您刚刚编辑的Renviron文件...完成!!

I was looking into this because R was having issues installing into the default location and was instead just putting the packages into the temp folder.我正在调查这个,因为 R 在安装到默认位置时遇到问题,而只是将包放入临时文件夹中。 It turned out to be the latest update for Mcaffee Endpoint Security which apparently has issues with R. You can disable the threat protection while you install the packages and it will work properly.事实证明,这是 Mcaffee Endpoint Security 的最新更新,它显然与 R 有问题。您可以在安装软件包时禁用威胁防护,它会正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM