如何更改R包的默认库路径

时间:2022-09-21 22:41:24

I have attempted to install R and R studio on the local drive on my work computer as opposed to the organization network folder because anything that runs through the network is really slow. When installing, the destination path shows that it's my local C:drive. However, when I install a new package, the default path shown is my network drive and there is no option to change:

我试图在我的工作计算机上的本地驱动器上安装R和R studio,而不是组织网络文件夹,因为任何通过网络运行的都非常慢。安装时,目标路径显示它是我的本地C:驱动器。但是,当我安装新软件包时,显示的默认路径是我的网络驱动器,并且没有更改选项:

.libPaths()
[1] "\\\\The library/path/I/don't/want"
[2] "C:/Program Files/R/R-3.2.1/library" 

I'm running windows 7 professional. How can I remove library path [1] and make path [2] my primary for all base packages and all new packages that I install?

我正在运行Windows 7专业版。如何删除库路径[1]并使路径[2]成为我安装的所有基础包和所有新包的主要路径?

3 个解决方案

#1


22  

See help(Startup) and help(.libPaths) as you have several possibilities where this may have gotten set. Among them are

请参阅帮助(启动)和帮助(.libPaths),因为您可能已经设置了几种可能性。其中有

  • setting R_LIBS_USER
  • 设置R_LIBS_USER
  • assigning .libPaths() in .Rprofile or Rprofile.site
  • 在.Rprofile或Rprofile.site中分配.libPaths()

and more.

和更多。

In this particular case you need to go backwards and unset whereever \\\\The library/path/I/don't/want is set.

在这种特殊情况下,您需要向后移动并取消设置\\\\库/路径/我/不/想要设置。

To otherwise ignore it you need to override it use explicitly i.e. via

要忽略它,你需要明确地覆盖它,即通过

library("somePackage", lib.loc=.libPaths()[-1])

when loading a package.

加载包时

#2


18  

Windows 7: If your C:\Program Files (or wherever R is installed) is blocked for writing, as mine is, then you'll get frustrated editing RProfile.site (as I did). As specified above, I updated R_LIBS_USER and it worked. However, even after reading the fine manual several times and extensive searching, it took me several hours to do this. In the spirit of saving someone else time...

Windows 7:如果您的C:\ Program Files(或者安装了R的地方)被阻止写入,就像我的那样,那么你会对编辑RProfile.site感到沮丧(正如我所做的那样)。如上所述,我更新了R_LIBS_USER并且它工作正常。然而,即使在阅读了好几本精细的手册并进行了广泛的搜索之后,我花了几个小时来做​​这件事。本着拯救别人时间的精神......

Let's assume you want your packages to reside in C:\R\Library:

假设您希望您的包驻留在C:\ R \ Library中:

  1. Create the folder C:\R\Library
  2. 创建文件夹C:\ R \ Library
  3. Click Start --> Control Panel --> User Accounts --> Change my environmental variables
  4. 单击开始 - >控制面板 - >用户帐户 - >更改我的环境变量
  5. The Environmental Variables window pops up. If you see R_LIBS_USER, highlight it and click Edit. Otherwise click New. Both actions open a window with fields for Variable and Value.
  6. 弹出“环境变量”窗口。如果看到R_LIBS_USER,请突出显示它并单击“编辑”。否则单击新建。这两个操作都打开一个窗口,其中包含Variable和Value字段。
  7. In my case, R_LIBS_USER was already there, and the value was a path to my desktop. I added to the path the folder that I created, separated by semicolon as mentioned above. C:\R\Library;C:\Users\Eric.Krantz\Desktop\R stuff\Packages. NOTE: I could have removed the path to the Desktop location and simply left C:\R\Library.
  8. 就我而言,R_LIBS_USER已经存在,并且值是我桌面的路径。我在路径中添加了我创建的文件夹,如上所述以分号分隔。 C:\ R \ Library; C:\ Users \ Eric.Krantz \ Desktop \ R stuff \ Packages。注意:我可以删除桌面位置的路径,只需离开C:\ R \ Library。

#3


6  

Facing the very same problem (avoiding the default path in a network) I came up to this solution with the hints given in other answers.

面对同样的问题(避免网络中的默认路径),我在其他答案中给出了提示。

The solution is editing the Rprofile file to overwrite the variable R_LIBS_USER which by default points to the home directory.

解决方案是编辑Rprofile文件以覆盖变量R_LIBS_USER,该变量默认指向主目录。

Here the steps:

这里的步骤:

  1. Create the target destination folder for the libraries, e.g., ~\target.
  2. 为库创建目标目标文件夹,例如〜\ target。
  3. Find the Rprofile file. In my case it was at C:\Program Files\R\R-3.3.3\library\base\R\Rprofile.
  4. 找到Rprofile文件。就我而言,它位于C:\ Program Files \ R \ R-3.3.3 \ library \ base \ R \ Rprofile。
  5. Edit the file and change the definition the variable R_LIBS_USER. In my case, I replaced the this line file.path(Sys.getenv("R_USER"), "R", with file.path("~\target", "R",.
  6. 编辑文件并将变量更改为R_LIBS_USER。在我的例子中,我用file.path(“〜\ target”,“R”,)取代了这行file.path(Sys.getenv(“R_USER”),“R”)。

The documentation that support this solution is here

支持此解决方案的文档就在这里

Original file with:

原始文件:

 if(!nzchar(Sys.getenv("R_LIBS_USER")))
     Sys.setenv(R_LIBS_USER=
                file.path(Sys.getenv("R_USER"), "R",
                          "win-library",
                          paste(R.version$major,
                                sub("\\..*$", "", R.version$minor),
                                sep=".")
                          )) 

Modified file:

修改文件:

if(!nzchar(Sys.getenv("R_LIBS_USER")))
     Sys.setenv(R_LIBS_USER=
                file.path("~\target", "R",
                          "win-library",
                          paste(R.version$major,
                                sub("\\..*$", "", R.version$minor),
                                sep=".")
                          ))

#1


22  

See help(Startup) and help(.libPaths) as you have several possibilities where this may have gotten set. Among them are

请参阅帮助(启动)和帮助(.libPaths),因为您可能已经设置了几种可能性。其中有

  • setting R_LIBS_USER
  • 设置R_LIBS_USER
  • assigning .libPaths() in .Rprofile or Rprofile.site
  • 在.Rprofile或Rprofile.site中分配.libPaths()

and more.

和更多。

In this particular case you need to go backwards and unset whereever \\\\The library/path/I/don't/want is set.

在这种特殊情况下,您需要向后移动并取消设置\\\\库/路径/我/不/想要设置。

To otherwise ignore it you need to override it use explicitly i.e. via

要忽略它,你需要明确地覆盖它,即通过

library("somePackage", lib.loc=.libPaths()[-1])

when loading a package.

加载包时

#2


18  

Windows 7: If your C:\Program Files (or wherever R is installed) is blocked for writing, as mine is, then you'll get frustrated editing RProfile.site (as I did). As specified above, I updated R_LIBS_USER and it worked. However, even after reading the fine manual several times and extensive searching, it took me several hours to do this. In the spirit of saving someone else time...

Windows 7:如果您的C:\ Program Files(或者安装了R的地方)被阻止写入,就像我的那样,那么你会对编辑RProfile.site感到沮丧(正如我所做的那样)。如上所述,我更新了R_LIBS_USER并且它工作正常。然而,即使在阅读了好几本精细的手册并进行了广泛的搜索之后,我花了几个小时来做​​这件事。本着拯救别人时间的精神......

Let's assume you want your packages to reside in C:\R\Library:

假设您希望您的包驻留在C:\ R \ Library中:

  1. Create the folder C:\R\Library
  2. 创建文件夹C:\ R \ Library
  3. Click Start --> Control Panel --> User Accounts --> Change my environmental variables
  4. 单击开始 - >控制面板 - >用户帐户 - >更改我的环境变量
  5. The Environmental Variables window pops up. If you see R_LIBS_USER, highlight it and click Edit. Otherwise click New. Both actions open a window with fields for Variable and Value.
  6. 弹出“环境变量”窗口。如果看到R_LIBS_USER,请突出显示它并单击“编辑”。否则单击新建。这两个操作都打开一个窗口,其中包含Variable和Value字段。
  7. In my case, R_LIBS_USER was already there, and the value was a path to my desktop. I added to the path the folder that I created, separated by semicolon as mentioned above. C:\R\Library;C:\Users\Eric.Krantz\Desktop\R stuff\Packages. NOTE: I could have removed the path to the Desktop location and simply left C:\R\Library.
  8. 就我而言,R_LIBS_USER已经存在,并且值是我桌面的路径。我在路径中添加了我创建的文件夹,如上所述以分号分隔。 C:\ R \ Library; C:\ Users \ Eric.Krantz \ Desktop \ R stuff \ Packages。注意:我可以删除桌面位置的路径,只需离开C:\ R \ Library。

#3


6  

Facing the very same problem (avoiding the default path in a network) I came up to this solution with the hints given in other answers.

面对同样的问题(避免网络中的默认路径),我在其他答案中给出了提示。

The solution is editing the Rprofile file to overwrite the variable R_LIBS_USER which by default points to the home directory.

解决方案是编辑Rprofile文件以覆盖变量R_LIBS_USER,该变量默认指向主目录。

Here the steps:

这里的步骤:

  1. Create the target destination folder for the libraries, e.g., ~\target.
  2. 为库创建目标目标文件夹,例如〜\ target。
  3. Find the Rprofile file. In my case it was at C:\Program Files\R\R-3.3.3\library\base\R\Rprofile.
  4. 找到Rprofile文件。就我而言,它位于C:\ Program Files \ R \ R-3.3.3 \ library \ base \ R \ Rprofile。
  5. Edit the file and change the definition the variable R_LIBS_USER. In my case, I replaced the this line file.path(Sys.getenv("R_USER"), "R", with file.path("~\target", "R",.
  6. 编辑文件并将变量更改为R_LIBS_USER。在我的例子中,我用file.path(“〜\ target”,“R”,)取代了这行file.path(Sys.getenv(“R_USER”),“R”)。

The documentation that support this solution is here

支持此解决方案的文档就在这里

Original file with:

原始文件:

 if(!nzchar(Sys.getenv("R_LIBS_USER")))
     Sys.setenv(R_LIBS_USER=
                file.path(Sys.getenv("R_USER"), "R",
                          "win-library",
                          paste(R.version$major,
                                sub("\\..*$", "", R.version$minor),
                                sep=".")
                          )) 

Modified file:

修改文件:

if(!nzchar(Sys.getenv("R_LIBS_USER")))
     Sys.setenv(R_LIBS_USER=
                file.path("~\target", "R",
                          "win-library",
                          paste(R.version$major,
                                sub("\\..*$", "", R.version$minor),
                                sep=".")
                          ))