Command Line Argument Parser for Rscript

What workflow and tools do you all use when developing R scripts that are run on a recurring basis?

Specifically,

  1. What command line argument parser do you use?
  2. How do you version control your development and production version of the scripts? How do you iteratively debug your R logic while having confidence it will work in non-interactive mode?

For 1) I am looking for a command line argument parsers that are named, typed and come with help message. I am aware of argparse and getopt which are great but has python dependencies. aarph is very neat and creative but I want the script self-documenting from the outside. Do you think there will be interests in wrapping a C++ CLI argument parser?

For 2) How does your workflow enable you to interactively work on an R script that is run from the command line? This is mine and I am curious to learn if you have better practices. In the below example, it allows me to source and debug code interactively while letting arguments to be sourced from the command line when running with Rscript.

filename <- "test.txt"
date <- "2018-03-01"

if (!interactive()) {
  args <- commandArgs(trailingOnly = TRUE)
  filename <- args[0]
  date <- args[1]
}

## work begins
## ...

p.s. I should acknowledge that parametrized Rmarkdown works wonders at times but sometimes I am just looking for a dull script instead of fancy reports.

Have you seen docopt?

https://cran.r-project.org/web/packages/docopt/index.html

1 Like

I have not. Thank you for sharing! Have you used it before? How do you like it?

My first concern is we need to learn the usage format to write the doc. I am not sure how robust the parser is (maybe it is super good.) I would prefer users to learn a package from autocompletion (and documentation of modular function as needed) instead of reading docs. I feel the magical moment of a good IDE and a well-designed package is when you just start typing what you think you need and function just pop up with intuitive arguments to fill the gap. Preferably each function does little things instead of a monolithic function :slight_smile:

Of course, I'm happy to hear otherwise that this package has lots of happy users.

I have only needed this functionality on 1-2 occasions but docopt got the job done nicely. I hope someone with more experience will weigh in.

You have a great serie of blog post by @sellorm on this subject here:
http://blog.sellorm.com/2017/12/18/learn-to-write-command-line-utilities-in-r/

The last post could be the one the most interesting as it customize the CLI. Moreover, it uses argparser :package: as it has no external dependencies. (different from argparse you mentionned.) Very neat for this kind of tools. The exemple in post 6 will show you the API.

On the list of other :package: not already mentioned, there are also optparse., optigrab.

You should try them out and find the one you feel at ease. (I am interesting by your feedback!)

4 Likes

argparser and optparse both check all my boxes at a glance. I will give the awesome blog post a read and mark that as a solution for now until I have some experience to share. Thanks a bunch to you both!

1 Like