Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R command line passing a filename to script in arguments (Windows)

I'm having a hard time passing a filename to my R script. The file is a csv file with the batch parameters for multiple runs of the script. I am trying to include it here so that the user does not need to edit the R script in order to specify the location of that file.

My Windows command line syntax is:

R CMD BATCH --slave "--args fn=batch.csv" myscript.r output.txt

The closest I have gotten to retrieving this in my R script is by doing:

eval(parse(file=commandArgs()[8])))
batch_args = read.table(fn, sep=",")

I have experimented with commandArgs(trailingOnly=TRUE) and parse(text=commandArgs()[8]), etc., with no luck. Most of the documentation that I have seen does not apply specifically to passing filenames. Can anyone think of a solution?

like image 955
Benjamin Avatar asked Jan 26 '11 18:01

Benjamin


1 Answers

As I said in my comment, I would use Rscript instead of R CMD BATCH:

Rscript myscript.R batch.csv

where myscript.R contains:

args <- commandArgs(TRUE)
batch_args <- read.table(args[1], sep=",")
# loop over multiple runs
like image 72
Joshua Ulrich Avatar answered Oct 07 '22 06:10

Joshua Ulrich