RStudio shows a different $PATH variable

丶灬走出姿态 提交于 2020-12-24 08:16:41

问题


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

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.

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

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?


回答1:


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.

According to that explanation:

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

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

System-wide environment variables are set by the Launch Agent and not by Bash. Exactly how you set the environment variables for applications launched from the Finder will depend on your version of Mac OS X. This answer might help.

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

You may have to restart the Finder:

killall -KILL Dock

Then restart RStudio.




回答2:


I have met the same problem. Instead of Lincoln's answer, I manually set the correct environment inside RStudio console by

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

in this case.

PATH that R is using could also checked in console by

Sys.getenv()



回答3:


I solved this issue (on MAC) by adding PATH to ~/.Renviron You can run this:

$ touch ~/.Renviron | R_PATH="PATH=$PATH" | echo $R_PATH >  ~/.Renviron

and restart Rstudio. Now you should have the same $PATH in both cases.



来源:https://stackoverflow.com/questions/31121645/rstudio-shows-a-different-path-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!