2.1 Using docopt Package

In R, we also have a nice implementation in a package called docopt, developed by Edwin de Jonge. Check out its tutorial on GitHub.

So let’s quickly create a command line interface for our R scripts with a dummy example. Let’s turn the uniform distribution function runif into a command line tool.

when you check out the help page for runif, here is the key information you want to markdown.

Usage
runif(n, min = 0, max = 1)
Arguments
n
number of observations. If length(n) > 1, the length is taken to be the number required.
min, max
lower and upper limits of the distribution. Must be finite.

I will add one more parameter to set seed, here is the R script file called runif.R.

At the beginning of the command line script, I use the docopt standard to write my tool help.

2.2 Quick Command Line Interface with commandArgs (Position and Named Args)

For advanced users, please read another tutorial “Creating Your Docker Container and Command Line Interface (with docopt)”, “docopt” is more formal way to construct your command line interface, but there is a quick way to make command line interface here using just commandArgs.

Suppose I already have a R script like this using position mapping the arguments

2.4 Executable Report with R Markdown (Advanced)

We cannot really make a R Markdown file executable in it by simply put

#!/bin/bash/Rscript

In your markdown document.

Of course, we can figure out a way to do it in liftr or knitr. But R Markdown allow you to pass parameters to your R Markdown template, please read this tutorial Parameterized Reports. This doesn’t solve my problem that I want to directly describe command line interface in the markdown template. However, here is alternative method:

Create an command line interface to pass params from docopt into rmarkdown::render() function. In this way, we can pass as many as possible parameters from command line interface into our R Markdown template.

So here we go, here is updated methods and it’s also what I use for another tutorial about RNA-seq workflow.

4 More Examples

There are more examples under inst/docker folder, you can check out how to describe command line and build Docker, how to make report template. You may read the online GitHub code. Or you could read another tutorial about how we wrap RNA-seq workflow from Bioconductor.