Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - Set environment variable permanently

Tags:

I'd like to set the default distribution of Python for my Reticulate package to use. I use,

Sys.setenv(RETICULATE_PYTHON = "/usr/local/bin/python3") 

however, I have to re-enter this line of code every time I start R. How can I set this permanently, so I don't need to specify which Python distribution I need every time?

like image 386
Christopher Costello Avatar asked Apr 09 '18 17:04

Christopher Costello


People also ask

How do I change my environment variables permanently?

To make permanent changes to the environment variables for all new accounts, go to your /etc/skel files, such as . bashrc , and change the ones that are already there or enter the new ones. When you create new users, these /etc/skel files will be copied to the new user's home directory.

Do environment variables persist?

Environment variables are variables that contain values necessary to set up a shell environment. Contrary to shell variables, environment variables persist in the shell's child processes.


1 Answers

On Windows, use Sys.getenv('R_USER') as @Brian Davis suggested in the comments to know the location of your home folder. On Linux, Sys.getenv('HOME') should be your normal home folder which you should use.

Now open up a terminal (if you're using recent versions of Rstudio there is one next to the console), go to your home folder and add a .Renviron file. You can do this without using the terminal too, but you'll probably have to confirm creation of a file starting with a dot.

cd path_to_my_home_Folder touch .Renviron 

Add RETICULATE_PYTHON = /usr/local/bin/python3 to it, and add also a new line at the end. Your file should look like this (if it's new):

> RETICULATE_PYTHON = /usr/local/bin/python3 

Now you should be able to access your environment variable with Sys.getenv('RETICULATE_PYTHON') at each R session, since R looks for any .Renviron file defining environment variables in R home folder at startup (see documentation for startup?Startup).

UPDATE 29/10/2018

As it turnouts the variables defined with .Renviron are available only within Rstudio, which is not so much of a surprise since the .Renviron file is read at Rstudio startup. If you want the environment variable to be available for Rscript (for instance), you can :

Windows Add it to your user environment variables, using the Modify environment variables utility (available in the Start menu search bar)

Mac You can do the exact same procedure as above but do that on your .bash_profile instead of .Rstudio. Open up a terminal and place yourself to your user root folder (default location of the terminal usually). Add the following line (without blanks around the equal sign):

export RETICULATE_PYTHON=/usr/local/bin/python3 

Save and close, restart terminal. The terminal reads your .bash_profile at start up, thus defining the environment variables. Your RETICULATE_PYTHON should now be available even in non-interactive R sessions.

like image 180
Romain Avatar answered Sep 17 '22 13:09

Romain