54

Is there any convenient way to automatically parse command line arguments passed to R scripts?

Something like perl's Getopt::Long?

David B
  • 29,258
  • 50
  • 133
  • 186

5 Answers5

64

There are three packages on CRAN:

  • getopt: C-like getopt behavior
  • optparse: a command line parser inspired by Python's optparse library
  • argparse: a command line optional and positional argument parser (inspired by Python's argparse library). This package requires that a Python interpreter be installed with the argparse and json (or simplejson) modules.

Update:

  • docopt: lets you define a command line interface by just giving it a description in the specific format. It is a port a docopt.py.
  • argparser: cross-platform command-line argument parser written purely in R with no external dependencies. This package is useful with the Rscript front-end and facilitates turning an R script into an executable script.
  • minimist: A binding to the minimist JavaScript library. This module implements the guts of optimist's argument parser without all the fanciful decoration (no external dependencies)
  • optigrab: parse options from the command-line using a simple, clean syntax. It requires little or no specification and supports short and long options, GNU-, Java- or Microsoft- style syntaxes, verb commands and more.
rcs
  • 67,191
  • 22
  • 172
  • 153
  • 5
    For some reason, `argparse` actually *requires* Python. Made me try `optparse` first... – krlmlr Jul 15 '13 at 13:49
  • 18
    unsolicited advice - as tempting as it is to use the outstanding python argparse package from within R, the cross-language dependency just makes your R script that much more complex and fragile. Don't do it. Use one of the pure-R options described above. – Chris Warth Feb 20 '15 at 18:19
  • 1
    Also, it seems like using the argparse library for R really slows down your script. – twb10 Jul 27 '19 at 18:23
  • 1
    `optigrab` has a [bug][https://github.com/decisionpatterns/optigrab/issues/1] in `opt_help()` which hasn't been resolved in >4 years despite a PR being provided. It makes it pretty unusable for the intended purpose. – Keiran Raine Jan 09 '23 at 14:58
37

The simplest way is to use commandArgs(). Example - save the code below as "options.R":

options <- commandArgs(trailingOnly = TRUE)
options

Run using "Rscript options.R x y z". Result:

[1] "x" "y" "z"

i.e. a list of 3 elements, one per argument.

David LeBauer
  • 31,011
  • 31
  • 115
  • 189
neilfws
  • 32,751
  • 5
  • 50
  • 63
12

Just to complement the Rscript answer:

edd@max:~$ r -e 'print(argv)' flim flam flom
[1] "flim" "flam" "flom"
edd@max:~$ 

We just use argv in littler. I had good luck with getopt, the older of the two available parsing packages.

JD Long
  • 59,675
  • 58
  • 202
  • 294
Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
6

May I introduce ArgumentParser in Xmisc package? It is a command line parser inspired by Python's argparse but it is Python-free.

http://cran.r-project.org/web/packages/Xmisc/vignettes/Xmisc-ArgumentParser.pdf

enter image description here

xb.
  • 1,617
  • 11
  • 16
1

The getopt long is in the --parameter=argument format like so

rscript script.R --parameter1=argument1 --parameter2=argument2

It can be parsed by just using basic string packages.

Example

cli.r

library(pracma)
library(stringr)
run.arguments <- commandArgs(TRUE)
valid.run.parameters <- c( "universe", "character", "ability" )
for ( i in 1:length( run.arguments ) ) {
    if ( strcmpi( substr( run.arguments[i], 1, 2 ), "--" ) & grepl( "=", run.arguments[i], fixed = TRUE) ) {
        key.pair <- str_split( run.arguments[i], "=", simplify=TRUE )
        run.parameter <- gsub( "--", "", key.pair[1] )
        run.argument <- key.pair[2]
        if ( run.parameter %in% valid.run.parameters ) {

            # DO YOUR MAGIC HERE! Here is an example...
            cat( run.parameter, "\n" )
            cat( run.argument,  "\n\n" )

        }
    }
}

The above script was written for brevity. See the more compelling version.

Usage

rscript cli.R --universe=MCU --character="Wade Wilson"

Output

universe
MCU

character
Wade Wilson
Abel Callejo
  • 13,779
  • 10
  • 69
  • 84