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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With