Linux 如何让 R 读取我的环境变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12291418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:15:45  来源:igfitidea点击:

How can I make R read my environmental variables?

linuxrubuntuamazon-ec2environment-variables

提问by JordanBelf

I am running R on EC2 spot instances and I need R to terminate the instance and cancel the spot request once the script has run.

我在 EC2 Spot 实例上运行 R,我需要 R 终止实例并在脚本运行后取消 Spot 请求。

For that I have set the "Request ID" into an environmental variable in /.bashrcand my plan was to simply call the following code into R once the script is ready

为此,我已将“请求 ID”设置为环境变量/.bashrc,我的计划是在脚本准备好后简单地将以下代码调用到 R 中

system("ec2-cancel-spot-instance-requests $SIR")

The issue I am having is that R is not "seeing" the same environmental variables I seen when I type envfrom outside R thus the command is not working.

我遇到的问题是 R 没有“看到”我env从 R 外部键入时看到的相同环境变量,因此该命令不起作用。

I have checked and if I set my environmental variables at /etc/environmentR is able to see those variables, but here is the other problem. As those variables are dynamic (the instance ID and the request ID is different each time a spot instance is created), I am running a script to create them in the form of:

我已经检查过,如果我在/etc/environmentR 中设置我的环境变量能够看到这些变量,但这是另一个问题。由于这些变量是动态的(每次创建 Spot 实例时,实例 ID 和请求 ID 都不同),因此我正在运行一个脚本以以下形式创建它们:

export SIR=`cat /etc/ec2_instance_spot_id.txt`

Where that file contains the dynamic ID

该文件包含动态 ID 的位置

So, how can I insert "dynamic" environmental variables into /etc/environment? Or, how can I make R read the environmental variables at /.bashrc?

那么,如何将“动态”环境变量插入到/etc/environment. 或者,如何让 R 读取环境变量/.bashrc

Any tip in the right direction will be much appreciated!

任何正确方向的提示将不胜感激!

采纳答案by Dirk Eddelbuettel

You want Sys.getenv()as in Sys.getenv("PATH"), say.

你想Sys.getenv()Sys.getenv("PATH"),说。

Or for your example, try

或者对于您的示例,请尝试

SIR <- Sys.getenv("SIR")   
system(paste("ec2-cancel-spot-instance-requests",  SIR))

As for setting variables at startup, see help(Startup)to learn about ~/.Renvironmentetc

至于在启动时设置变量,参见help(Startup)了解~/.Renvironment

回答by Thorsten

Using Sys.getenv()you see all variables listed in the current environment.

使用Sys.getenv()您可以看到当前环境中列出的所有变量。

However, they are different from those used in your current shell, for example specified in .profile.

但是,它们与您当前 shell 中使用的不同,例如在 .profile 中指定的那些。

To set the variables for R create a .Renvironfile in your home directory and write there

要设置 R 的变量,请.Renviron在您的主目录中创建一个文件并在那里写入

MYDIRECTORY="/home/wherever"

After restarting R you will be able to access this variable with

重新启动 R 后,您将能够访问此变量

Sys.getenv("MYDIRECTORY")

回答by cheevahagadog

I'm pretty new to R but my approach was this: I had project-level environment variables stored in a .envfile. To make it accessible in R, I used

我对 R 很陌生,但我的方法是这样的:我将项目级环境变量存储在一个.env文件中。为了使其在 R 中可访问,我使用了

> readRenviron(".env")

Then to access a specific variable

然后访问特定变量

> Sys.getenv("RDS_UID")

And it worked perfectly.

它工作得很好。