R-永久设置环境变量 [英] R - Set environment variable permanently

查看:317
本文介绍了R-永久设置环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的Reticulate软件包设置Python的默认发行版.我用,

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

但是,每次启动R时,我都必须重新输入这一行代码.如何永久设置此代码,所以不必每次都指定我需要的Python发行版?

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?

推荐答案

在Windows上,按照注释中@Brian Davis的建议使用Sys.getenv('R_USER')来知道主文件夹的位置.在Linux上,Sys.getenv('HOME')应该是您应该使用的普通主文件夹.

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.

现在打开一个终端(如果您使用的是Rstudio的最新版本,则在控制台旁边有一个终端),转到您的主文件夹并添加一个.Renviron文件.您也可以在不使用终端的情况下执行此操作,但是您可能必须确认以点开头的文件的创建.

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

向其中添加RETICULATE_PYTHON = /usr/local/bin/python3,并在末尾添加新行.您的文件应如下所示(如果是新文件):

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

现在,您应该可以在每个R会话中使用Sys.getenv('RETICULATE_PYTHON')访问环境变量,因为R在启动时会在R home文件夹中查找定义环境变量的任何.Renviron文件(请参见startup ?Startup的文档).

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

更新2018年10月29日

事实证明,用.Renviron定义的变量仅在Rstudio中可用,这并不奇怪,因为.Renviron文件是在Rstudio启动时读取的.如果希望环境变量可用于Rscript(例如),则可以:

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 ,使用Modify environment variables实用程序(在开始"菜单搜索栏中提供)将其添加到用户环境变量中

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

Mac 您可以执行与上面完全相同的过程,但是在您的.bash_profile而不是.Rstudio上执行.打开终端,然后将自己置于用户根文件夹(通常是终端的默认位置).添加以下行(等号周围没有空格):

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

保存并关闭,重新启动终端.终端在启动时会读取您的.bash_profile,从而定义了环境变量.现在,即使在非交互式R会话中,您的RETICULATE_PYTHON也应该可用.

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.

这篇关于R-永久设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆