Motivation from RStudio
RStudio has a great set of expanding features including a terminal, Git integration, and a new Connections tab. Almost enough to make me want to switch from using Emacs as my editor/IDE/operating system. One of the many nice features of RStudio is the Knit button for rendering R Markdown (.Rmd) documents to output formats like HTML or PDF (or even Microsoft Word).
My Previous Workflow
Recently I have been writing a lot of R markdown documents to create reproducible reports at work. The rmarkdown package makes it very easy. You just need to run the
rmarkdown::render() function to knit an .Rmd file to markdown and subsequently render the output file (PDF, Word, HTML, etc.) with a single command!
My R Markdown files would always have a chunk at the top which was not evaluated or run and just held the command to run in R to render the output.
So anytime I wanted to render the document I would search for this block, use
C-c C-c to run it in the current R process, and then wait for the command to finish in R before making my next edit (fixing an error, adding more chunks, etc.).
This strategy is sub-optimal for a few reasons
- It takes multiple, repetitive keystrokes to reverse search, execute the code, and pop the mark back to the original place
C-r render RET C-c C-c C-u C-SPC
- The code gets run in whatever R process the script is currently associated with. This is a minor issue but I prefer the rendering to happen in a “fresh” R process to avoid bugs or issues from interactively defined variables.
Finding a Better Way
I wasn’t sure if support for a better R Markdown workflow was already included in ESS (and I’m trying to dabble in elisp) so I wrote my own elisp function to render the document.
I shared this function in an answer on Stack Overflow. The accepted answer is good and uses
polymode-weave but I prefer to use
rmarkdown::render() which uses knitr and pandoc under the hood.
So - what does my function (
spa/rmd-render) actually do? It simply builds a string containing the R command (in the variable
rcmd), pipes that to R using a shell command (the variable
command) and runs it using the
Note that I have some specific parameter settings like
output_dir = '../reports' because I always have a consistent directory structure and want my report in a particular directory but the elisp can be easily customized to suit your needs by calling it with a prefix argument (e.g.,
C-u). I use this for example when I want to write the same report to multiple output formats that I have specified in my header. I can just type
C-u C-c r and change the
output_format from ‘pdf_document’ to ‘all’. The function also has a history - so typing
C-u C-c r followed by
M-p will cycle through history of previous render commands.
With this in your init file, you only need to type
C-c r from inside your .Rmd file (or
C-u C-c r to render to a different format, location, etc.). The command will open a new window with a buffer called compilation where any errors will appear.
This could definitely be improved and some may still prefer the built in command
polymode-weave but I find that this makes me much more efficient. In any case, it was fun to practice coding in Emacs lisp. The most useful thing I learned was that the
compile function (
M-x compile) can be used for more than just running
make. It actually will work with any shell command!