R - Set environment variable permanently R - Set environment variable permanently r r

R - Set environment variable permanently


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_Foldertouch .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.


The packge usethis has a function that opens the file .Renviron of your home folder.

usethis::edit_r_environ()

Once the file is opened, you just need add your pair key=value, save and close it.

RETICULATE_PYTHON=/usr/local/bin/python3