Collaborator-specific commands in R

When you're sharing an R script with someone, sometimes you both need to run commands that only make sense for you and on your system. How do you manage it?

One way is to have your own system-specific file that you run before the shared script. But this doesn't really solve the problem... you still want that script version controlled so that it is safe and your work is reproducible.

Fortunately, you can set up environment variables to help handle it.

Add the following line

R_COMPUTER_ID="your_name"

to the .Renviron file. This is located at:

  • ~/.Renviron on Linux
  • C:\Program Files\R\rw1051\etc\Renviron.site (example) on Windows (I haven't tested this)
  • ~/.Renviron on Mac (I haven't tested this either)

Begin your shared R script with stanza:

computer_id<-Sys.getenv('R_COMPUTER_ID')
if(computer_id=="your_name"){
  setwd('/home/your_name/projects/this_project')
} else if(computer_id=="collaborator_name") {
  setwd('/home/collaborator_name/stuff/stuff2012/Rthings')
  start_hamsters()
}

Modifying the Renviron.site file would work as well, but then all users on your system would inherit the R_COMPUTER_ID variable and you would need sudo access.

links