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?
Dirk’s answer here is everything you need. Here’s a minimal reproducible example.
I made two files:
exmpl.batandexmpl.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.pngwith some plotexmpl.batchwith 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
RScriptandRterm:Rscripthas simpler syntaxRscriptautomatically chooses architecture on x64 (see R Installation and Administration, 2.6 Sub-architectures for details)Rscriptneedsoptions(echo=TRUE)in the .R file if you want to write the commands to the output file