RStudio 显示不同的 $PATH 变量 [英] RStudio shows a different $PATH variable
问题描述
可能的重复问题,但是不知道RStudio的启动过程来判断问题是否相同.我在 Linux 上使用 RStudio Desktop v0.99.442.从 RStudio 内的控制台,我运行
Possible duplicate question, but I don't know the startup process of RStudio to tell if the problems are the same. I'm using RStudio Desktop v0.99.442 on Linux. From the console inside RStudio, I run
system("echo $PATH")
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
然而,我真正的 $PATH
(当我从控制台 echo $PATH
时看到的)上面有很多我想要的其他程序能够使用 R 中的 system() 进行调用.例如,在我的 .bashrc 中,我将 tabix 附加到 $PATH
.
However, my real $PATH
(which I see when I echo $PATH
from the console) has a lot of other programs on it that I'd like to be able to call using system() from R. For example, in my .bashrc, I have tabix appended to $PATH
.
奇怪的是,当我从控制台中的 R 会话运行相同的命令时,我得到
The weird thing is when I run the same command from a R session within the console, I get
system("echo $PATH")
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games;/path/to/bcftools-1.2/htslib-1.2.1/tabix
这是正确的.因此,RStudio 中的 R 会话和控制台中的 R 会话之间存在差异.它们不加载相同的环境变量.如何让 RStudio 使用正确版本的 $PATH?
which is correct. So there is a difference between a R session inside RStudio and a R session in the console. They do not load the same environment variables. How do I get RStudio to use the correct version of $PATH?
推荐答案
当你从命令行启动 R 然后运行 system(echo $PATH)
时,你从你的命令行会话.例如,当您从 Mac 上的 Dock 或 Finder 或作为 Ubuntu 中的系统应用程序而不是从命令行启动 RStudio 时,RStudio 不会从您的 /.bashrc
获取其环境.相反,它将从系统范围的设置中获取环境变量.它如何找到这些系统设置将取决于操作系统.
When you start R from the command line and then run system(echo $PATH)
, you are inheriting the Bash environment from your command line session. When you launch RStudio from, say, the Dock or Finder on a Mac or as a system application in Ubuntu, and not from the command line, RStudio does not gets its environment from your /.bashrc
. Instead it will get the environment variables from system-wide settings. How it finds those system settings will depend on the operating system.
在Ubuntu中查看环境变量的解释,尤其是桌面应用程序部分.
See this explanation of environment variables in Ubuntu, especially the section on desktop applications.
根据那个解释:
您可以通过编辑应用程序的 .desktop 文件向应用程序添加环境变量.例如,要运行环境变量为 APPMENU_DISPLAY_BOTH=1 的digiKam",找到对应的 digikam.desktop 文件,并通过 env 命令将变量的设置添加到Exec"条目中:
You can add an environment variable to an application by editing its .desktop file. For example, to run "digiKam" with the environment variable APPMENU_DISPLAY_BOTH=1, find the corresponding digikam.desktop file and add the setting of the variable, via the env command, to the entry "Exec":
RStudio .desktop
文件将位于 ~/.local/share/applications/
或 /usr/share/applications/
(最有可能是后者).编辑该文件以包含此行:
The RStudio .desktop
file will be in ~/.local/share/applications/
or /usr/share/applications/
(most likely the latter). Edit that file to include this line:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games;/path/to/bcftools-1.2/htslib-1.2.1/tabix
Mac
系统范围的环境变量由启动代理设置,而不是由 Bash 设置.为从 Finder 启动的应用程序设置环境变量的确切方式取决于您的 Mac OS X 版本.这个 答案可能有帮助.
我这样做的方法是添加一个文件,~/Library/LaunchAgents/com.username.envvariables.plist
,其中包含以下内容:
The way that I do this is to add a file, ~/Library/LaunchAgents/com.username.envvariables.plist
with these contents:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.username.envvariables</string>
<key>ProgramArguments</key>
<array>
<string>sh</string>
<string>-c</string>
<string>
launchctl setenv PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games;/path/to/bcftools-1.2/htslib-1.2.1/tabix
</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
然后您必须加载该文件:
You will then have to load that file:
launchctl load ~/Library/LaunchAgents/com.username.envvariables.plist
您可能需要重新启动 Finder:
You may have to restart the Finder:
killall -KILL Dock
然后重新启动 RStudio.
Then restart RStudio.
这篇关于RStudio 显示不同的 $PATH 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!