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

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

问题描述

我正在EC2竞价型实例上运行R,并且我需要R终止实例并在脚本运行后取消竞价请求.

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.

为此,我在/.bashrc中将请求ID"设置为环境变量,而我的计划是在脚本准备好后将以下代码简单地调用到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可以看到这些变量,但这是另一个问题.由于这些变量是动态的(每次创建竞价型实例时,实例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("PATH")中那样使用Sys.getenv().

或者以您的示例为例,

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天全站免登陆