views:

82

answers:

3

I've been using R in Ubuntu to make system calls using system() for things like spinning up Amazon EC2 instances, managing files on S3, etc. If I start R from the command line everything works fine. But if I start R from a script using Rscript, or from ESS, I have issues with environment variables not being set.

I think this is an issue with me not properly grokking where to set environment variables in Ubuntu. I thought the "right place" (for some definition of "right") was to set user environment variables in ~/.bashrc. This is where I set things like export EC2_HOME=/home/jd/ec2 but when I execute R from ESS and make system calls, the .bashrc script is not being run. I've tried Googing about and I see many an exegesis on environment variables in Ubuntu, such as this one. My knee jerk reaction is to try each recommendation in the aforementioned thread and stop giving a shit as soon as one of the options works. But then I end up with non-standard settings which bite me in the ass later.

So how should I set environment variables so that they are properly set when I run a system() call in R?

+5  A: 

You can try to set them in R itself using Sys.setenv.

Joshua Ulrich
well shit. I was trying to do this through the Ubuntu approach and didn't realize there was an R approach. Thank you!
JD Long
A: 

You can force the system to read your .bashrc file by using the source command

source ~/.bashrc

Lots of inelegant and ugly ways to apply this

Brian Douglas Smith
+5  A: 

I think you are confusing the issue. I fear this may be about login shells versus non-login shells. See the bash manual page for the fine print ... which has driven me bonkers in the past.

That said, if you can set environment variables system-wide, you have a few options:

  • /etc/environment is a very good place as it is shell-agnostic should you ever use a different shell
  • for login versus non-login shells, the one way to get complete control that I found suitable was to put my changes into something like ~/.local_bashrc
  • the add . ~/.local_bashrc from and and all of

    • ~./bashrc
    • ~/.bash_profile
    • ~/.profile`

    etc pp.

You can precede the sourcing with a echo Hello from FILE where you replace FILE with the name of the file. That shows you the difference between shells starting from login (eg via gdm et al), via ssh connection, via new xterm etc terminals and so on.

Dirk Eddelbuettel
Damn good tips, as always Dirk. I was tempted to start editing `/etc/environment` but every time I start to screw around with something in `/etc/` I hear a German accented voice say very softly 've are not learning very fast, are ve?' and I kinda get creeped out and reconsider my decision to leave the family farm and pursue life in the big city.
JD Long