9

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?

Benjamin
  • 11,560
  • 13
  • 70
  • 119

4 Answers4

18

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
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • With this exact syntax, I get "Error: could not find function "batch_args". If I put a print(args) statement, "batch.csv" is there, however. – Benjamin Jan 26 '11 at 18:53
  • I don't mind using Rscript, but can I get the output written to a file similar to R CMD BATCH --slave? Why is Rscript better? – Benjamin Jan 26 '11 at 18:59
  • @Benjamin: I think you copied and/or pasted something wrong. There's no way the code I posted would give you that error because that error requires something like `batch_args()`. – Joshua Ulrich Jan 26 '11 at 19:01
7

Besides using Rscript (as Josh said) you should also use the CRAN packages getopt or optparse as they were written for this very purpose.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
6

What do you mean by 'no luck'? The filename is in there, in the commandArgs() function, you just have to work out how to get it out. Code and error messages are handy.

That's not a problem if the only extra argument is a filename, you know its position. What will confuse you is when you start having more complex argument passing.

You're also complicating things with passing 'fn=foo.csv'. Just pass the filename and assign it to fn in your script. If you really want to use eval you probably need to quote your filename, thus myscript.r is:

ca = commandArgs(trailingOnly=TRUE)
eval(parse(text=ca))
print(read.csv(fn))

and run thus:

R  --slave "--args fn='batch.csv'" < myscript.r
     A B C
   1 1 2 3
   2 6 8 3

Where batch.csv is a simple csv file.

You could do a loop over "ca" in your script and eval everything. It's slightly dangerous though, since you could easily break basic functionality.

Personally I'd loop over ca, look for name=value pairs for a known set of names, and set them. Basically implementing getopt, but someone has probably done that already...

Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • Almost works, but breaks when trying to do read.table because "batch.csv" is not recognized as a proper filename but as string. I though giving it the full path might help, but then it breaks because of spaces. Currently, I run it from the directory where all files are located (.r, .txt and .csv). – Benjamin Jan 26 '11 at 18:41
  • There's no such thing as a 'proper filename'. R stores filenames in strings. fn="batch.csv"; read.csv(fn) will read from the file batch.csv. – Spacedman Jan 26 '11 at 18:44
  • okay, my example now reads from a csv file as proof-of-concept. – Spacedman Jan 26 '11 at 18:46
  • When I run the exact syntax from above R --slave "--args fn='batch.csv'" < myscript.r, I get the warning unknown option "--args fn='batch.csv'". – Benjamin Jan 26 '11 at 18:48
  • I'm guessing that's Windows' command line passing being a pain. Same with R CMD BATCH etc as in your original? – Spacedman Jan 26 '11 at 18:52
  • My bad, had an error later in the code. Finally found it when I tried print(read.table(fn)) which worked fine. Thanks for the help. – Benjamin Jan 26 '11 at 19:22
  • @Spacedman "--args fn='batch.csv'" is hardly a Windows issue. Everything inside the quotes becomes a single arg. That's how it works as you know well. – David Heffernan Jan 26 '11 at 21:17
  • 1
    I don't know how COMMAND.COM bothers to treat single or double quotes. Second thing I do on Windows installs is to install Cygwin. First thing is install Firefox in order to get Cygwin. – Spacedman Jan 26 '11 at 22:50
  • @Spacedman: I do the exact same thing. Nice to know I'm not alone. – Joshua Ulrich Jan 27 '11 at 02:52
2

try

fn="batch.csv"; R CMD BATCH --slave "--args $fn" myscript.r output.txt
WCC
  • 1,922
  • 1
  • 14
  • 7