Passing Parameters to an R Script via CMD or Batch
You can do all of this in R using one of these packages to parse command-line options:
- docopt (my favourite)
- optparse
- argparse
- getopt
or doing it manually -- not recommended.
You also do not want the older R CMD BATCH
-- use Rscript (or littler, but littler does not work on Windows).
Code Example
#!/usr/bin/Rscript
suppressMessages(library(docopt))
doc <- "Usage: foo.R [-h] [-x] [--src REPODIR] [--out OUTDIR] [FILES...]
-s --src REPODIR source root directory [default: ~/git]
-o --out OUTDIR output directory [default: /tmp]
-h --help show this help text"
opt <- docopt(doc) # docopt parsing
print(opt)
Use with -h
You get a nice message, automatically, with not formatting need:
edd@rob:/tmp$ Rscript so50256138.R -h
Usage: foo.R [-h] [-x] [--src REPODIR] [--out OUTDIR] [FILES...]
-s --src REPODIR source root directory [default: ~/git]
-o --out OUTDIR output directory [default: /tmp]
-h --help show this help text
edd@rob:/tmp$
Use with argument
Note how one default argument is used, and the other from the command-line:
edd@rob:/tmp$ Rscript so50256138.R -s A
List of 9
$ --src : chr "A"
$ --out : chr "/tmp"
$ --help: logi FALSE
$ -x : logi FALSE
$ FILES : list()
$ src : chr "A"
$ out : chr "/tmp"
$ help : logi FALSE
$ x : logi FALSE
NULL
You can access them in opt
by name or by option flag.
The docopt site has more; this is actually a portable specification and the CRAN package implements it for R.
Passing arguments to an R script from command lines when the argument is a string
Here is a simple R script which would take string inputs:
args <- commandArgs(trailingOnly = TRUE)
cat(args, sep = "\n")
I save the file as "test.R" in my home directory. In the command line I can then use:
Rscript test.R "file.txt"
The "
are optional if your string does not have whitespace. But this would be recognised as two inputs:
Rscript test.R file 1.txt
There is a nice little tutorial from which I took this here.
how to pass command line argument that is a matrix to R CMD BATCH
I don't think matlab can pass data to the stdin
of a command called with system
. Your options then are:
- Write the data out to a file in a way that can be read in within the Rscript process (e.g. csv). This could potentially be a temporary file, and the filename could be passed as the argument to the R script.
- Encode the data in a cross-program way that can be passed as a command-line argument (e.g. JSON). However, this is likely to be much harder to implement successfully, and you may run into issues of maximum line length for commands.
- Encode the data as an R expression that can be evaluated within R (e.g.
matrix(c(1, 4, 5, 2, 9, 7), nrow = 3)
along the lines of the question you yourself linked to .
Unless there's a very good reason, I'd go with option 1.
How to pass string as command line arguments while using R CMD BATCH
The code R CMD BATCH --no-save --no-restore '--args a=0 mydir="/home/test"' test.R
works.
Remember add args <- commandArgs(trailingonly=TRUE)
to the program at the beginning. That's why my program reported an error, I forgot to add it to my program.
Passing SLURM batch command line arguments to R
The export parameter has to be passed to sbatch not to the run.sh script.
It should be like this:
sbatch --export=basePath=‘a’ run.sh
Is it possible to pass an entire list as a command line argument in R
I think optparse
accepts only one argument for each "flag". I would use a comma-separated list:
R -f myscript.R --people James,John,Emily,Anna
And in your script you get the character string "James,John,Emily,Anna"
that you can split with strsplit
to get the vector of names.
How can I pass arguments to an Rscript i have in my desktop?
You have your R script (test.R), for example:
#commandArgs picks up the variables you pass from the command line
args <- commandArgs(trailingOnly = TRUE)
print(args)
Then you run your script from the command line using:
#here the arguments are 5 and 6 that will be picked from args in the script
PS C:\Users\TB\Documents> Rscript .\test.R 5 6
[1] "5" "6"
Then what you get back is a vector containing 2 elements, i.e. 5 and 6. trailingOnly = TRUE
makes sure you just get back 5 and 6 as the arguments. If you omit it then the variable args will also contain some details about the call:
Check this for example. My R script is:
args <- commandArgs()
print(args)
And the call returns:
PS C:\Users\TB\Documents> Rscript .\test.R 5 6
[1] "C:\\Users\\TB\\scoop\\apps\\anaconda3\\current\\lib\\R\\bin\\x64\\Rterm.exe"
[2] "--slave"
[3] "--no-restore"
[4] "--file=.\\test.R"
[5] "--args"
[6] "5"
[7] "6"
I didn't include the trailingOnly = TRUE
here and I got some call details returned as well.
Related Topics
How to Use Objects from Global Environment in Rstudio Markdown
Get Values and Positions to Label a Ggplot Histogram
Tidyverse Pivot_Longer Several Sets of Columns, But Avoid Intermediate Mutate_Wider Steps
Pass a Vector of Variable Names to Arrange() in Dplyr
What Is "Object of Type 'Closure' Is Not Subsettable" Error in Shiny
Remove Ids That Occur X Times R
Is There a Better Alternative Than String Manipulation to Programmatically Build Formulas
Date Conversion from Posixct to Date in R
Different Size Facets Proportional of X Axis on Ggplot 2 R
Options for Caching/Memoization/Hashing in R
Twitter, Roauth and Windows: Register Ok, But Certificate Verify Failed
Why Does R Use Partial Matching
Proper Idiom for Adding Zero Count Rows in Tidyr/Dplyr
Filter Data Frame Rows Based on Values in Vector
R Compare Multiple Values with Vector and Return Vector