Emacs Org-mode and literate programming

2014-08-06

I've been using Emacs for editing and evaluating R code with ESS for a long time now. I also like Emacs for editing statistical reports and compiling them using knitr (and before that, Sweave), using plain LaTeX or just RMarkdown. Now, I'm getting interested in org-mode as an alternative to noweb, which I previously used when looking for a way to integrate different programming languages (e.g., sh, sed, and R) into the same document.

Literate programming

I've long been interested by literate programming (LP), and I wrote some posts about different aspects of LP. I've seen an increased interest in LP and reproducible research this year (see, e.g., Roger Peng's blog posts or handouts on Coursera and elsewhere). At some point, it get even mixed up with the new kid on the block: Data Science, which really does mean nothing to me since I consider myself as a data scientist as soon as I have to clean and process data coming from medical research. (If this has to do with big data or the computer scientist vs. statistician debate, I'm not really interested in that: After all, we are data slaves and we must deal with that.)

R and knitr

There are so many good tutorials and posts on R + knitr that I feel too lazy to search through my Twitter posts or web bookmarks. Here are two recent ones:

The updated RMarkdown (v2, brought to you by http://www.rstudio.com) now relies on Pandoc to build HTML, PDF, or even DOCX documents. They all are standalone documents, meaning that you don't have to provide a separate folder with figures in the case of HTML, for example. This is pretty good. I've been using knitr for two years now, and I updated my statistical workflow to build custom reports based on knitr. I also wrote some custom Elisp to edit/build RMarkdown files under Emacs. It should not be too difficult to update this code in order to make use of RStudio rmarkdown::render() instead of knitr::knit(). Furthermore, as discussed on GitHub, it is possible to define custom renderer, for example in, e.g., our ~/.Rprofile:

my_render <- function(input, encoding) {
    rmarkdown::render(input, clean = FALSE, encoding = encoding)
}

or use YAML to set knitr opts and hooks.

Org-mode

Organize your life in plain text offers a good overview of what is org-mode, and why people who cares about plain text workflow should care about Emacs/Org. I must admit I personally remain on very pragmatic approaches to programming (I need code to do something right now because I'm a medical statistician, not a programmer, but it has to be reproducible at some point--I don't want to spend too much time rewriting it two years ago when a reviewer asks for further analyses) and editing (I like plain text because I can view and update it on almost every device: a laptop, my iPhone, etc., and I can use any of all the magic GNU utilities to process text streams).

Org-mode for literate programming

I remember that people were already using org-mode to display live R code in their TeX document before the first release of knitr. Surely we could find several threads on Stack Overflow or Cross Validated around 2010-2012. As of Markdown vs. Org or LP through org-mode, here are some related threads, but see also Bernd Weiss's Slides from the R Benutzer Treffen Köln / R user meeting in Cologne on "A brief introduction to reproducible research with Emacs’ Org mode and R". There is also How to Use Emacs Org-Babel Mode to Write Literate Programming Document in R Language available on http://orgmode.org.

Two years ago, Eric Schulte and coll. published a paper entitled A Multi-Language Computing Environment for Literate Programming and Reproducible Research in the Journal of Statistical Software (46(3), 2012). This article provides a good review of exiting systems for LP, as well as some example of use of Org-mode with various programming languages, including R (p. 20 ff.). In fact, this is Babel (formerly, org-babel) that is responsible for processing chunks of code into a text file for matted using org directives. There are some user-submitted examples that helps to see how Babel can be used in interaction with R. It looks like ox-ravel.org, from orgmode-accessories is now considered as a potential alternative to Sweave or knitr for processing R code under Emacs. At least, it has been used to produce an R vignette, see Writing R vignettes in emacs org mode using ox-ravel.

Regarding Clojure, I found the following post, Using Clojure with org-babel and nREPL, where the author described how to configure Emacs to run Clojure code through nREPL. However, nREPL has been replaced by CIDER, so this is no longer applicable except for those who didn't make the switch. In the meantime, Org-babel-clojure has been updated and can be used with the development version of org-mode.

---

Articles with the same tag(s):

Multi-Group comparison in Partial Least Squares Path Models
Yet another gray theme for R base graphics
Writing a book
R, pipes and Co.
R Graphs Cookbook
A modular configuration for Emacs
Common lisp on Mavericks
user2014
Reproducible research with R
Scheme and Emacs

---