[R] export variable from bash to R

Krzysztof Mitko kmitko at mm.st
Mon Oct 29 17:57:23 CET 2012

W dniu 29.10.2012 11:43, sophie pisze:
> 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
> 	Rscript -e 'source("myscript.R")' 
> done

VARIABLES=(a b c d)
for i in ${VARIABLES[@]}
	Rscript myscript.R $i

> 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.

When loading the script with "source", the output won't show up unless
you explicitely print it (see ?source). Maybe it's executed but you just
don't see the output?

> Any help will be greatly appreciated!

Best regards,
Krzysztof Mitko

More information about the R-help mailing list