如何让 R 读取我的环境变量? [英] How can I make R read my environmental variables?

查看:21
本文介绍了如何让 R 读取我的环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

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

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

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

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

I have checked and if I set my environmental variables at /etc/environment R 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:

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

该文件包含动态 ID 的位置

Where that file contains the dynamic ID

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

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

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

Any tip in the right direction will be much appreciated!

推荐答案

你想要 Sys.getenv()Sys.getenv("PATH") 一样,说.

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

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

Or for your example, try

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

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

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

这篇关于如何让 R 读取我的环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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