Skip to content

Easily Expose R Functions to Command Line Arguments

License

Notifications You must be signed in to change notification settings

jeremystan/aargh

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

aargh


Easily Expose R Functions to Command Line Arguments.

This package provides an easy wrapper for automagically converting any R function into a command line driven application. It is inspired by easyargs in Python, and uses the R argparse library to access the Python argparse parser.

Installation

You can install this package directly from github using:

library(devtools)
install_github('jeremystan/aargh')

Usage

For example, consider the simple example app example.R:

library(aargh)

#' Repeat text multiple times, comma delimited
repeat_text <- function(text = 'hi', times = 1L) {
  message <- paste(rep(text, times), collapse = ', ')
  writeLines(message)
}

aargh(repeat_text)

Then we can run this app as an Rscript:

$ Rscript example.R
hi
$ Rscript example.R --times 10
hi, hi, hi, hi, hi, hi, hi, hi, hi, hi
$ Rscript example.R --times 5 --text bye
bye, bye, bye, bye, bye

Functions supported

This only works for functions that:

  • Have constant primitive defaults for all arguments (used to infer type)
  • Supported types are numeric, integer, character or logical

If you want to work with a function that has arguments without defaults, you can simply write a wrapper function that provides a default for those arguments. For example, consider the sample.R example:

library(aargh)

#' Wrap the base sample function to specify the size type
wrap_sample <- function(size = 1L) {
  sample(size)
}

aargh(wrap_sample)

Then from the command line:

$ Rscript sample.R
1
$ Rscript sample.R --size 10
 [1]  8  3  2  7  4  9  1  6 10  5

Note that you must use the full syntax --arg_name to change an argument.

For logical arguments, you can use syntax like --flag FALSE or --flag F or --flag False, similarly --flag TRUE, --flag T and --flag True all work. But the flag does not work in isolation (e.g., --flag will complain it is missing an argument instead of defaulting to TRUE).

About

Easily Expose R Functions to Command Line Arguments

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages