[R] export variable from bash to R

sophie melanie.bieli at bluewin.ch
Mon Oct 29 11:43:54 CET 2012


Dear R experts

This probably seems very easy to you guys, but I'm a beginner and would be
really glad if someone helped me with this:
I am trying to automate the execution of an R script (let's call it
"myscript.R") by passing a variable from a bash script to myscript.R. 
I know I can use the command Rscript, but I don't know how to declare in
bash which variable will be accessed by the "commandArgs" command in
myscript.R.

So my bash script looks about like this:

#!/bin/bash

VARIABLES=( a b c d )

for i in ${VARIABLES[@]}; do
	VARIABLENAME=$i
	Rscript -e 'source("myscript.R")' 
done

In myscript.R, I would like to use the current VARIABLENAME when executing
the program, i.e.,
myscript <- function()	{

args <- commandArgs(TRUE)     # args should now be set to either a,b,c, or d
load(paste("/home/user/../../", args, ".RData", sep=""))    #  this defines
the path to the data file that will be used in this run
                                                                                                   
...further commands...
}

At the moment, myscript.R doesn't seem to be executed at all when I execute
the bash script.
Any help will be greatly appreciated!

Thanks,
sophie







--
View this message in context: http://r.789695.n4.nabble.com/export-variable-from-bash-to-R-tp4647749.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list