Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make R read my environmental variables?

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.

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

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.

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` 

Where that file contains the dynamic ID

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!

like image 782
JordanBelf Avatar asked Sep 06 '12 00:09

JordanBelf


People also ask

How can I see environment in R?

RStudio by default displays four panes: Console, Source Code, Environment/History, and Files. You can rearrange them by going to View -> Panes -> Pane Layout. You can add and remove tabs from panes by going to View and selecting/deselecting tab options listed at the bottom.

How do I read all environment variables?

To list all the environment variables, use the command " env " (or " printenv "). You could also use " set " to list all the variables, including all local variables.

Which is used to read environment variable?

The command env displays all environment variables and their values.


1 Answers

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

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

like image 75
Dirk Eddelbuettel Avatar answered Oct 14 '22 05:10

Dirk Eddelbuettel