Being a HipstR: the coolest R packages you've never heard of

Kieran R Campbell
September 4th 2017

aargh

How do you pass command line arguments to R?

$ Rscript myscript.R 1 two three
args <- commandArgs(trailingOnly = TRUE)
myfunc(args[1], args[2])

Input validation

arg1 <- as.numeric(args[1])
arg2 <- args[2]
# etc

aargh

You might want to have named arguments

$ R myscript.R --verbose --output_directory .
args <- commandArgs(trailingOnly = TRUE)
for(i in seq_along(args)) {
  if(args[i] == "--verbose") {
    verbose <- TRUE
  } 
  if(args[i] == "--output_directory") {
    output_dir <- args[i+1] # <--- with additional checks of course!
  }
}

The aargh package

https://github.com/jeremystan/aargh

  • Define a function with arguments with defaults
  • Pass this function to arrgh(...)
  • The package takes care of passing command line args correctly

aargh example

Function that repeats text multiple times:

repeat_text <- function(text = 'hi', 
                        times = 1L) {
  message <- paste(rep(text, times), 
                   collapse = ' ')
  writeLines(message)
}
library(aargh)
aargh(repeat_text)
$ Rscript example.R --times 10 --text bye

aargh

repeat_text <- function(text = 'hi', times = 1L)

Command line:

$ 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

Imagine I have publication quality plots

print(plot1)

plot of chunk unnamed-chunk-8

print(plot2)

plot of chunk unnamed-chunk-9

cowplot

https://www.github.com/wilkelab/cowplot + CRAN

library(cowplot)
print(plot1)

plot of chunk unnamed-chunk-11

plot_grid: the best function ever

plot_grid(plot1, plot2)

plot of chunk unnamed-chunk-12

plot_grid: labels

plot_grid(plot1, plot2, labels = "AUTO")