Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing command line arguments in R scripts

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

Something like perl's Getopt::Long?

like image 214
David B Avatar asked Aug 08 '10 08:08

David B


People also ask

How do I read command-line arguments in R?

Use the commandArgs() Function to Retrieve Arguments in R Arguments passed on the command line should be retrieved using the commandArgs() function. We will use it with the argument trailingOnly=TRUE . The function returns a character vector.

What is argument parsing?

Argument Parsing using sys.Your program will accept an arbitrary number of arguments passed from the command-line (or terminal) while getting executed. The program will print out the arguments that were passed and the total number of arguments.

Which module is not used for parsing command line arguments automatically?

Getopt is not used for parsing command-line arguments automatically.

What is argument parsing in Python?

argparse — parse the arguments. Using argparse is how you let the user of your program provide values for variables at runtime. It's a means of communication between the writer of a program and the user. That user might be your future self.


3 Answers

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.
like image 164
rcs Avatar answered Oct 11 '22 02:10

rcs


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.

like image 40
neilfws Avatar answered Oct 11 '22 03:10

neilfws


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.

like image 26
Dirk Eddelbuettel Avatar answered Oct 11 '22 04:10

Dirk Eddelbuettel