# An R-based Research Notebook

I recently set up a fork of the Octopress blogging software to generate posts that contain explanatory text, LaTex math, and output from R code.

## Motivation

For my research I’ve been preparing weekly reports to send out before meetings. For me at least, writing things out really cements concepts and usually helps to filter out a lot of the cruft of poorly thought out ideas. I started by emailing out pdfs I generated using a combination of knitr and multimarkdown.
The problem with emailing pdfs is that I needed to keep track of all the old reports, they weren’t indexed, and I couldn’t generate things like cross links between reports.

What I really wanted was a blog. I’ve been really liking Octopress as the platform for this blog.
I like the fact it creates static pages which I can then upload to my web account at my University and put them behind a password so only my supervisors can have access. It also keeps all old reports around, allows links among posts, and generates lists of posts for each category.

The project is set up as a fork of Octopress so if you’re interested in using it the project as well as the installation instructions are at https://github.com/torsneyt/r-notebook.

## Example post

So, what does the output look like? Here the plots are generated by R during execution and automatically linked into the post. The math is handled by MathJax.

## Combining the pieces

I wanted to keep using knitr and MathJax as before. They work really well for my purposes.

I recently found out that version 0.7 of knitr supports executing languages other than R which is fantastic! I haven’t had a chance to try it out yet so I’m not sure how well it works. There’s a demo page for it here: http://yihui.name/knitr/demo/engines/

Adding MathJax to Octopress was just a simple matter of adding a link to the MathJax javascript file to the page template in Octopress.

The major additions are written as 2 plugins: multimarkdown.rb, which adds multimarkdown support to Octopress, and knitr.rb, which runs all the blog posts through knitr to execute the R code and generate the plots and such before the final mmd to html conversion.

### mmd plugin

The original version is here. The only real change I made was that the extension is now multimarkdown. I found that because octopress/jekyll’s extension mapping will match partial extensions mmd was being detected as a different file type than multimarkdown.

### knitr

The knitr plugin consists of 2 files knitr.rb which is just a wrapper for knit_markdown.R which does most of the work.

#### knitr.rb

Here’s the code for knitr.rb. It uses tempfiles instead of just sending the text directly to knitr so that we can index the cache by blog post name. That way there’s a unique cache directory for each blog post and identical cache section names in different blog posts won’t clobber each other.

#### knit_markdown.R

This is the script that does most of the heavy lifting. Extensions to knitr’s processing is handled through various “hooks.” These are described in the knitr manual.

Lines 9–15 set up the cache and image directories that knitr will use. Lines 28–66 is an extension to support movies of multiple R plots. In order to get Octopress to highlight R code we need to wrap it in liquid codeblock tags. The hook for that is done by lines 69–71. The rest just sets all the hooks I want to use and renders the files using knitr.

## Conclusion

And that’s about it. The rest of the changes are in the repository of course.
Feel free to fork the repository for your own work and let me know what you think!