38

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?

TylerH
  • 20,799
  • 66
  • 75
  • 101
JordanBelf
  • 3,208
  • 9
  • 47
  • 80
  • I found that variables in ~/.profile were accessible from Sys.getenv(), but not from ~/.bashrc. I had to do `source ~/.profile`, then `R` from a terminal to get it working after adding new variables. – wordsforthewise Nov 28 '17 at 20:38

4 Answers4

40

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

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • 3
    Thanks a lot Dirk for your time. The issue I am having is that when I do for example `names(s <- Sys.getenv())` then `SIR` is not there, when I do `SIR <- Sys.getenv("SIR")` there is nothing in SIR, but when I exit R and I do `env` that list of environmental variables is totally different than the one I get when I `Sys.getenv()` and there I can see SIR and others I need. I will give a look at `Renvironment`, thanks a lot again. – JordanBelf Sep 06 '12 at 01:04
  • 1
    OK, this is shameful.Why do I want to read the environmental variables within R if I already created 2 files with the request ID and instance ID? I can just get the value of those files and perform my task... this does not explain why R was not seeing the same environmental variables as I see when I quit R, but will get me going! Thanks for the inspiration! – JordanBelf Sep 06 '12 at 01:45
  • 6
    I made this work by using `Sys.setenv()` and basically copying my environmental variables from outside R to R. Although this makes my script work, I wish I could understand why there is whole set of different environmental variables witting R – JordanBelf Sep 06 '12 at 02:15
17

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

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

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

MYDIRECTORY="/home/wherever"

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

Sys.getenv("MYDIRECTORY")
Thorsten
  • 331
  • 2
  • 6
  • My home directory does not have the .Renviron file, and inside R it returns an old value of the environment variable after I have change the value several days ago. Could not figure out where R gets the old value and not returning the new value. – Kemin Zhou Apr 25 '18 at 18:02
17

My approach was this: I had project-level environment variables stored in a .env file. To make it accessible in R, I used

> readRenviron(".env")

Then to access a specific variable

> Sys.getenv("RDS_UID")

And it worked perfectly.

TylerH
  • 20,799
  • 66
  • 75
  • 101
cheevahagadog
  • 4,638
  • 3
  • 15
  • 15
0

A more complete approach:

Make a file like myenvs/.Renviron with contents:

USERNAME="my_username"
PASSWORD="StrongPassword"

Then, load and use in R like:

readRenviron("myenvs/.Renviron")
username <- Sys.getenv("USERNAME")
password <- Sys.getenv("PASSWORD")
Paul
  • 3,920
  • 31
  • 29