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

发布于 2025-01-13 17:13:01 字数 1205 浏览 3 评论 0原文

我在 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.site 文件并添加以下行

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

但是,在执行此操作并启动 RStudio 后,这是我得到的输出

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

我添加到 Rprofile.site.libPaths 命令似乎没有任何效果!为什么会这样呢?或者更重要的是,如何解决问题,以便无需输入库位置即可安装和加载软件包?

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

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

不是很奇怪吗?

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

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.

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

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" 

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?

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"

Isn't that strange?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(17

最好是你 2025-01-20 17:13:02

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

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

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.

touch ~/.Renviron
echo "R_LIBS_USER=/custom/path/in/absolute/form" >> ~/.Renviron
浴红衣 2025-01-20 17:13:02

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

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

just change the default folder for your R libraries in a directory with no Administrator rights, e.g.

.libPaths("C:/R/libs")
静待花开 2025-01-20 17:13:02

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

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

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

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

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

阅读 help(Startup) 很有用。

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.

Uninstallled both R and Rstudio, creating directories C:\R and C:\Rstudio then reinstalled both.

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. Open Rstudio, errors should be gone.

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.

To see what R_LIBS_USER is set to:
?Sys.getenv()

Reading help(Startup) is useful.

红焚 2025-01-20 17:13:02

如果在安装新版本的 R 或通过任何其他方式后您的默认包库已更改,您可以在以下命令的帮助下附加这两个库以使用所有包。
获取现有的库路径:

.libPaths()

现在,设置现有的旧路径:

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

希望有帮助。

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.
Get the existing library path :

.libPaths()

Now,set the existing and the old path :

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

Hope it helps.

吻泪 2025-01-20 17:13:02

我遇到了同样的问题,遇到了 this 。如果您想创建另一个位置,c("C:/Users/mynewlocation") 也应该可以工作。正如此处所述“您应该能够右键单击 Rstudio.exe 图标,单击“属性”,然后选择一个选项以始终以管理员身份运行 Rstudio,请确保每次打开 Rstudio 时都使用相同的图标。”

    myPaths <- .libPaths()   # get the paths
myPaths <- c(myPaths[2], myPaths[1])  # switch them
.libPaths(myPaths)  # reassign them

I had the same problem and I run into this. If you want to create another location c("C:/Users/mynewlocation") should be working as well. As mentioned in here "You should be able to right-click on the Rstudio.exe icon, click Properties, and select an option to always run Rstudio as administrator. Be sure you use that same icon whenever you want to open Rstudio."

    myPaths <- .libPaths()   # get the paths
myPaths <- c(myPaths[2], myPaths[1])  # switch them
.libPaths(myPaths)  # reassign them
梦巷 2025-01-20 17:13:02

我读了自述文件。他们提到在命令行中使用 .libPaths() 来检查存在哪些路径。我之前有 2 个库路径。当我在我想要的地方使用命令 .libpath("C:/Program Files/R/R-3.2.4revised/library") 时,它更改了库路径。当我再次在命令行输入 .libPaths() 时,它显示了正确的路径。希望这有帮助

I read the readme. In that they mentioned use .libPaths() in command line to check which paths are there. I had 2 library paths earlier. When I used the command .libpath("C:/Program Files/R/R-3.2.4revised/library") where I wanted, it changed the library path. When I typed in .libPaths() at the command line again it showed me the correct path. Hope this helps

滥情稳全场 2025-01-20 17:13:02

由于这里的大多数答案都与 Windows 和 Windows 相关。 Mac OS,(考虑到我也遇到过这个问题)我决定发布帮助我在我的 Arch Linux 设置上解决这个问题的过程。

第 1 步:

  • 在您的系统中进行全局搜索(例如 ANGRYSearch),查找术语 Renviron(这是配置文件,其中包含设置用户库)。
  • 它应该在以下目录路径中仅返回两个结果:
    1. <代码>/etc/R/
    2. /usr/lib/R/etc/
      注意: Renviron 配置文件存储在 1 和 2 处。 2(上面)彼此热链接(这意味着当保存正在编辑的文件时,对一个文件所做的更改将自动[以相同的形式/结构]应用于另一个文件 - [您还需要 sudo 保存文件编辑后的权限])

第 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-'~/Apps/R/rUserLibs'}

第 3 步:

  • 保存您创建的 Renviron 文件刚刚编辑...完成!!

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.

Step 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:
    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 ] ).

Step 2:

  • Navigate into the 1st directory path ( /etc/R/ ) and open the Renviron file with your favourite text editor.
  • 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.

    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:

  • Save the Renviron file you've just edited ... DONE !!
时光匆匆的小流年 2025-01-20 17:13:02
getwd()
# [1] "C:/Users/..../software/My R studio"

使用双引号复制上面的链接

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

您的默认路径将更改以安装软件包

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

不及他 2025-01-20 17:13:02

如果您想永久更改库路径(无需每次进入 R 时调用 .libPath() ,这对我有用:

  1. 在您的主目录下创建 .Rprofile。(~/.Rprofile)

  2. 类型。
    .libPaths(c( .libPaths(), "你的新路径" ))
    在 .Rprofile 文件中,保存。

  3. 打开R(任何目录)并检查,只需输入.libPaths(),你可以发现你的libaray路径已更新!

If you want to change your library path permanently (without calling .libPath() every time when entering in R, this works for me:

  1. create .Rprofile under your home directory. (~/.Rprofile)

  2. type
    .libPaths(c( .libPaths(), "your new path" ))
    in .Rprofile file, save.

  3. open R (any directory) and check, just type .libPaths(), you can find your libaray path is updated!

紫瑟鸿黎 2025-01-20 17:13:02

我知道这听起来很疯狂,但问题(如果您使用的是 Windows)是您没有以管理员身份启动 R Studio。

如果您单击下拉菜单选择安装包位置,您就会知道这是您的,并且它不会让您选择其他位置。

关闭 R Studio 即可。找到该应用程序。右键单击它并“以管理员身份运行”,

然后您将能够更改安装位置。

I know this is going to sound crazy, but the issue (if you're one Windows) is that you didn't launch R Studio as an administrator.

You'll know this is you if you click the dropdown menu to choose your install packages location and it won't let you choose a different one.

Just close R Studio. Find the app. Right click it and "Run as administrator"

You will then be able to change the installation location.

起风了 2025-01-20 17:13:02

我正在研究这个问题,因为 R 在安装到默认位置时遇到问题,而是只是将包放入临时文件夹中。事实证明,这是 Mcaffee Endpoint Security 的最新更新,显然与 R 存在问题。您可以在安装软件包时禁用威胁防护,它将正常工作。

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

过期以后 2025-01-20 17:13:01

正确的解决方案是将环境变量 R_LIBS_USER 设置为所需库文件夹的文件路径值,而不是让 RStudio 识别 Rprofile.site 文件。

要在 Windows 中设置环境变量 R_LIBS_USER,请转到控制面板(系统属性 -> 高级系统属性 -> 环境变量 -> 用户变量)为所需的值(库的路径)文件夹),例如,

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

如果由于某种原因您无法访问控制面板,您可以尝试在 Windows 上从命令行运行 rundll32 sysdm.cpl,EditEnvironmentVariables 并添加从那里的环境变量。

设置 R_LIBS_USER 将确保该库首先显示在 .libPaths() 中,无论是直接启动 RStudio 还是通过右键单击文件并“打开方式”来启动 RStudio。

如果始终通过单击 RStudio 快捷方式启动 RStudio,则 Rprofile 解决方案可以工作。在这种情况下,将默认工作目录设置为存放 Rprofile 的目录< /a> 就足够了。单击文件启动 RStudio 时,Rprofile 解决方案不起作用,因为这会将工作目录更改为默认工作目录。

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.

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), e.g.

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.

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.

The Rprofile solution can work if RStudio is always started by clicking the RStudio shortcut. In this case, setting the default working directory to the directory that houses your Rprofile will be sufficient. 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.

蓝梦月影 2025-01-20 17:13:01

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

.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 成为放置新包的第一个地方):

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

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

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

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

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

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

由于 Sys.getenv() 返回 R 进程的当前系统环境,因此您可以使用以下命令查看库和其他路径:

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

对于存储和访问包来说重要的两个是(现在在 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 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):

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

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

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.

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())) ]

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/
 
不…忘初心 2025-01-20 17:13:01

我通过将代码放置在默认工作目录的 .Rprofile 文件中成功解决了该问题。

首先,我找到了默认工作目录的位置

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

然后我使用文本编辑器编写了一个简单的.Rprofile文件,其中包含以下行

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

最后,当我启动R时并运行 .libPaths() 我得到了所需的输出:

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

I managed to solve the problem by placing the code in the .Rprofile file in the default working directory.

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

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

Finally, when I start R and run .libPaths() I get the desired output:

> .libPaths()
[1] "C:/software/Rpackages"               "C:/Program Files/R/R-2.15.2/library"
[3] "C:/Program Files/RStudio/R/library"
‘画卷フ 2025-01-20 17:13:01

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

对于 Windows 7 操作系统:

  1. 在您想要存放个人库的驱动器上创建一个目录,例如 C:\User\Rlibs(或您有权访问的其他目录)

  2. 在 Windows 中搜索/转到“编辑您的帐户的环境变量”用于编辑控制面板设置的搜索栏

  3. 单击“环境变量”窗口中间的“新建...”

  4. 在“新建用户变量”窗口中,键入“变量名称”R_LIBS,以及变量的路径您创建的个人库目录,例如C:\User\Rlibs

  5. 单击“确定”,您应该在“用户变量”窗口中看到变量/值对

  6. 再次单击“确定”

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

对于 Mac:

  1. 在“Home”或“用户名”目录中创建一个名为 Rlibs 的文件夹

  2. 启动终端应用程序

  3. 类型:echo "R_LIBS=~/Rlibs" > .Renviron 确保拼写和大小写匹配。

  4. 输入 ls -a 查看目录中文件的完整列表,其中现在应包括 .Renvrion

  5. 验证 .Renviron文件已正确设置:more .Renviron

启动 R/Rstudio 并输入 .libPaths(),您应该会看到通往您个人图书馆的新路径。

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. I hadn't seen it laid out this explicitly on SO, so I summarized the steps they provided, for Windows and then for Mac.

For a Windows 7 OS:

  1. 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)

  2. Search for/go to "Edit environment variable for your account" in the Windows search bar to edit control panel settings

  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, e.g. 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.

For Mac:

  1. In your "Home" or "username" directory create a folder called Rlibs

  2. Launch the Terminal application

  3. Type: echo "R_LIBS=~/Rlibs" > .Renviron Make sure the spelling and case matches.

  4. Type ls -a to see the full list of files in the directory, which should now include .Renvrion

  5. Verify that the .Renviron file has been set properly: more .Renviron

Launch R/Rstudio and type .libPaths() and you should see the new path to your personal library.

夏末染殇 2025-01-20 17:13:01

如果您没有管理员权限,打开位于 \R-3.1.0\etc 中的 Rprofile.site 文件并添加

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

:在启动时直接评估 .libPath() 命令

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:

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

This evaluates the .libPath() command directly at start

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文