I've got a R script for which I'd like to be able to supply several command-line parameters (rather than hardcode parameter values in the code itself). The script runs on Windows.
I can't find info on how to read parameters supplied on the command-line into my R script. I'd be surprised if it can't be done, so maybe I'm just not using the best keywords in my Google search...
Any pointers or recommendations?
you need littler (pronounced 'little r')
Dirk will be by in about 15 minutes to elaborate ;)
A few points:
Command-line parameters are accessible via
commandArgs()
, so seehelp(commandArgs)
for an overview.You can use
Rscript.exe
on all platforms, including Windows. It will supportcommandArgs()
. littler could be ported to Windows but lives right now only on OS X and Linux.There are two add-on packages on CRAN -- getopt and optparse -- which were both written for command-line parsing.
Edit in Nov 2015: New alternatives have appeared and I wholeheartedly recommend docopt.
Dirk's answer here is everything you need. Here's a minimal reproducible example.
I made two files:
exmpl.bat
andexmpl.R
.exmpl.bat
:Alternatively, using
Rterm.exe
:exmpl.R
:Save both files in the same directory and start
exmpl.bat
. In the result you'll get:example.png
with some plotexmpl.batch
with all that was doneYou could also add an environment variable
%R_Script%
:and use it in your batch scripts as
%R_Script% <filename.r> <arguments>
Differences between
RScript
andRterm
:Rscript
has simpler syntaxRscript
automatically chooses architecture on x64 (see R Installation and Administration, 2.6 Sub-architectures for details)Rscript
needsoptions(echo=TRUE)
in the .R file if you want to write the commands to the output fileAdd this to the top of your script:
Then you can refer to the arguments passed as
args[1]
,args[2]
etc.Then run
If your args are strings with spaces in them, enclose within double quotes.
I just put together a nice data structure and chain of processing to generate this switching behaviour, no libraries needed. I'm sure it will have been implemented numerous times over, and came across this thread looking for examples - thought I'd chip in.
I didn't even particularly need flags (the only flag here is a debug mode, creating a variable which I check for as a condition of starting a downstream function
if (!exists(debug.mode)) {...} else {print(variables)})
. The flag checkinglapply
statements below produce the same as:where
args
is the variable read in from command line arguments (a character vector, equivalent toc('--debug','--help')
when you supply these on for instance)It's reusable for any other flag and you avoid all the repetition, and no libraries so no dependencies:
Note that in
flag.details
here the commands are stored as strings, then evaluated witheval(parse(text = '...'))
. Optparse is obviously desirable for any serious script, but minimal-functionality code is good too sometimes.Sample output:
Try library(getopt) ... if you want things to be nicer. For example: