是的,您可以阻止创建R
目录 -R是可配置的通过一组环境变量。
然而,正确设置这些并不简单。第一个问题是许多 R 软件包对其安装的 R 版本很敏感。如果您升级 R 并尝试加载现有包,它可能会崩溃。因此,R包库路径应该特定于R版本。
在集群上,另一个问题是相同的库路径可能会被运行在不同架构上的不同集群节点读取。这种情况很少见,但确实发生了。在这种情况下,编译的 R 包可能需要根据架构而有所不同。
因此,一般来说,R 库路径需要特定于 R 版本和系统架构。
接下来,即使配置替代路径R会默默地忽略它如果它不存在。因此,请务必手动创建您已配置的目录。
最后,这个配置应该放在哪里?一种选择是将其放入用户环境文件,可以用环境变量指定其路径R_ENVIRON_USER
- 默认为$HOME/.Renviron
。但这并不理想,因为这意味着用户在调用 R: 此文件中的变量时无法临时覆盖此设置override调用环境。
Instead, I recommend setting this in the user profile (e.g. $HOME/.profile
). However, when you use a desktop launcher to launch your RStudio, this file won’t be read, so be sure to edit your *.desktop file accordingly.1
总之,将以下内容添加到您的$HOME/.profile
:
export R_LIBS_USER=${XDG_DATA_HOME:-$HOME/.local/share}/R/%p-library/%v
并确保该目录存在:re-source~/.profile
(在当前 shell 内启动一个新 shell是不足够的),并执行
mkdir -p "$(Rscript -e 'cat(Sys.getenv("R_LIBS_USER"))')"
The above is using the XDG base dir specification, which is the de-facto standard on Linux systems.2 The path is using the placeholders %p
and %v
. R will fill these in with the system platform and the R version (in the form major.minor
), respectively.
如果您想使用自定义 R 配置文件(“用户配置文件”)和/或 R 环境文件,我建议通过配置以相同的方式设置它们的位置R_PROFILE_USER
and R_ENVIRON_USER
(因为它们的默认位置再次位于用户主目录中):
export R_PROFILE_USER=${XDG_CONFIG_HOME:-$HOME/.config}/R/rprofile
export R_ENVIRON_USER=${XDG_CONFIG_HOME:-$HOME/.config}/R/renviron
1 I don’t have a Linux desktop system but I believe that editing the Env
entry to the following should do it:
Exec=env R_LIBS_USER=${XDG_DATA_HOME:-$HOME/.local/share}/R/%p-library/%v /path/to/rstudio
2 Other systems require different handling. On macOS, the canonical setting for the library location would be $HOME/Library/Application Support/R/library/%v
. However, setting environment variables on macOS for GUI applications is frustratingly complicated.
在 Windows 上,规范位置是%LOCALAPPDATA%/R/library/%v
。要设置此变量,请使用[Environment]::SetEnvironmentVariable在 PowerShell 中或者使用时cmd.exe
, use setx.