How can I get R to read my environment variables?


Jordan Bell

I'm running R on an EC2 spot instance and need R to terminate the instance and cancel the spot request after the script runs.

For this I have set the "request id" in an environment variable /.bashrcand my plan is to simply call the following code into R once the script is ready

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

The problem I'm having is that R can't "see" the same environment variables that I see when typing envfrom outside R , so the command doesn't work.

I've checked and I /etc/environmentcan see the variables if I set the environment variable to R, but that's another question. Since these variables are dynamic (the instance ID and request ID are different each time a Spot Instance is created), I am running a script to create them in the form:

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

The file contains the location of the dynamic ID

So, how to insert "dynamic" environment variables /etc/environment? Alternatively, how do I get R to read environment variables /.bashrc?

Any hints in the right direction would be greatly appreciated!

Dirk Edby Bootle:

You want to Sys.getenv()be there Sys.getenv("PATH"), say.

Or take your example as an example

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

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

Related


How can I read my Google location history in R

Ajay Ohri I found my Google Location History at https://maps.google.co.uk/locationhistory/b/0/?hl=zh-CN Then, I downloaded the KML file I installed rgdalit correctly, but I can't read the file I put the file name and layer name in https://gis.stackexchange.com

How can I read my Google location history in R

Ajay Ohri I found my Google Location History at https://maps.google.co.uk/locationhistory/b/0/?hl=zh-CN Then, I downloaded the KML file I installed rgdalit correctly, but I can't read the file I put the file name and layer name in https://gis.stackexchange.com

How can I get sudo to read my ~/.gemrc file?

Dave I'm using Ruby on Rails 5 on Debian Linux, Apache and Passenger. I created the file ~/.gemrc gem: --no-rdoc --no-ri However, I am running the command sudo bundle install And it doesn't seem to accept the above. How do I get "sudo" to recognize my "~/.ge

How can I read my Google location history in R

Ajay Ohri I found my Google Location History at https://maps.google.co.uk/locationhistory/b/0/?hl=zh-CN Then, I downloaded the KML file I installed rgdalit correctly, but I can't read the file I put the file name and layer name in https://gis.stackexchange.com

How can I make sudo keep my environment variables?

aknuds1 Using sudo 1.7.4p4 on Solaris 5.10 and sudo 1.6.7p5 on RHEL4 u6, I don't see how to preserve environment variables such as $PYTHONPATH. I've added this line to sudoers but it doesn't make any difference: Defaults !env_reset Am I doing something wrong,

How can I read my Google location history in R

Ajay Ohri I found my Google Location History at https://maps.google.co.uk/locationhistory/b/0/?hl=zh-CN Then, I downloaded the KML file I installed rgdalit correctly, but I can't read the file I put the file name and layer name in https://gis.stackexchange.com

How can I pass variables from my post to my get?

programmer man I am using react/express. I have a simple web page that takes user input and uses axios.post. In my backend, I need to create a file in my get. I haven't been able to get the return function to work, I don't want to use a global variable as it h

How can I get R to read my environment variables?

Jordan Bell I'm running R on an EC2 spot instance and need R to terminate the instance and cancel the spot request after the script runs. For this I have set the "request id" in an environment variable /.bashrcand my plan is to simply call the following code i

How can I get sudo to read my ~/.gemrc file?

Dave I'm using Ruby on Rails 5 on Debian Linux, Apache and Passenger. I created the file ~/.gemrc gem: --no-rdoc --no-ri However, I am running the command sudo bundle install And it doesn't seem to accept the above. How do I get "sudo" to recognize my "~/.ge

How can I read my Google location history in R

Ajay Ohri I found my Google Location History at https://maps.google.co.uk/locationhistory/b/0/?hl=zh-CN Then, I downloaded the KML file I installed rgdalit correctly, but I can't read the file I put the file name and layer name in https://gis.stackexchange.com

How can I make sudo keep my environment variables?

aknuds1 Using sudo 1.7.4p4 on Solaris 5.10 and sudo 1.6.7p5 on RHEL4 u6, I don't see how to preserve environment variables such as $PYTHONPATH. I've added this line to sudoers but it doesn't make any difference: Defaults !env_reset Am I doing something wrong,

How can I get sudo to read my ~/.gemrc file?

Dave I'm using Ruby on Rails 5 on Debian Linux, Apache and Passenger. I created the file ~/.gemrc gem: --no-rdoc --no-ri However, I am running the command sudo bundle install And it doesn't seem to accept the above. How do I get "sudo" to recognize my "~/.ge