The joys of Rscript and parsing commandArgs

I’ve had a hacky, but workable, setup using a mixture of m4 and R CMD BATCH to run jobs on our local grid varying the parameters each time. Today I switched to Rscript, and it’s been a breeze.

At the top of my .R file I put

args <- getArgs()

and I put the getArgs() function below in my ~/.Rprofile.

Then I can run the script using

./myfile.R --args myarg=something

and in the .R file refer to args$myargs. –args is used to separate arguments you want to pass to R from those you want to be available in your script.

Continue reading