RStudio shows another $ PATH variable

Possible duplicate question, but I don’t know how to start the RStudio process to determine if the problems are the same. I am using RStudio Desktop v0.99.442 on Linux. On 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 many other programs that I would like to use using system () from R. For example, in my .bashrc, I added tabix to $PATH .

Strange when I run the same command from an R session inside 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 

what is right. So there is a difference between an R session inside RStudio and an 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?

+7
r rstudio path-variables
source share
2 answers

When you start R from the command line and then run system(echo $PATH) , you will inherit the Bash environment from the command line session. When you start RStudio, say Dock or Finder on a Mac or as a system application in Ubuntu, and not from the command line, RStudio does not get its environment from your /.bashrc . Instead, it will receive environment variables from system-wide parameters. How he discovers that these 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 this explanation:

You can add an environment variable to the application by editing the .desktop file. For example, to start "digiKam" with the environment variable APPMENU_DISPLAY_BOTH = 1, find the corresponding digikam.desktop file and add the variable parameter via the env command to the "Exec" entry:

The RStudio .desktop file will be located in ~/.local/share/applications/ or /usr/share/applications/ (most likely the latter). Edit this 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, not Bash. Exactly how you set environment variables for applications running from Finder will depend on your version of Mac OS X. This answer may help .

The way I do this is to add the file ~/Library/LaunchAgents/com.username.envvariables.plist with this content:

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

Then you need to download this file:

 launchctl load ~/Library/LaunchAgents/com.username.envvariables.plist 

You may need to restart Finder:

 killall -KILL Dock 

Then restart RStudio.

+13
source share

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

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

and restart Rstudio. You should now have the same $ PATH in both cases.

0
source share

All Articles