R - Set environment variable permanently

后端 未结 1 599
日久生厌
日久生厌 2021-01-01 17:14

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\")


        
相关标签:
1条回答
  • 2021-01-01 18:10

    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.

    0 讨论(0)
提交回复
热议问题