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

查看:52
本文介绍了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 会查找任何定义的 .Renviron 文件启动时 R 主文件夹中的环境变量(请参阅启动文档?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 中可用,这并不奇怪,因为在 Rstudio 启动时会读取 .Renviron 文件.如果您希望环境变量可用于 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天全站免登陆