Welcome to the twelveth post in the randomly relevant R recommendations series, or R4 for short. This post will insert a short diversion into what was planned as a sequence of posts on faster installations that started recently with this post but we will resume to it very shortly (for various definitions of "very" or "shortly").
Earlier today Davis Vaughn posted a tweet about a blog post of his describing a (term) paper he wrote modeling bitcoin volatilty using Alexios’s excellent rugarch package—and all that typeset with the styling James and I put together in our little pinp package which is indeed very suitable for such tasks of writing (R)Markdown + LaTeX + R code combinations conveniently in a single source file.
Leaving aside the need to celebreate a term paper with a blog post and tweet, pinp is indeed very nice and deserving of some additional exposure and tutorials. Now, Davis sets out to do all this inside RStudio—as folks these days seem to like to limit themselves to a single tool or paradigm. Older and wiser users prefer the flexibility of switching tools and approaches, but alas, we digress. While Davis manages of course to do all this in RStudio which is indeed rather powerful and therefore rightly beloved, he closes on
I wish there was some way to have Live Rendering like with blogdown so that I could just keep a rendered version of the paper up and have it reload every time I save. That would be the dream!
and I can only add a forceful: Fear not, young man, for we can help thou!
Modern operating systems have support for epoll and libnotify, which can be used from the shell. Just how your pdf application refreshes automagically when a pdf file is updated, we can hook into this from the shell to actually create the pdf when the (R)Markdown file is updated. I am going to use a tool readily available on my Linux systems; macOS will surely have something similar. The
entr command takes one or more file names supplied on
stdin and executes a command when one of them changes. Handy for invoking
make whenever one of your header or source files changes, and useable here. E.g. the last markdown file I was working on was named
comments.md and contained comments to a referee, and we can auto-process it on each save via
echo comments.md | entr render.r comments.md
render.r from littler (new release soon too…; a simple
Rscript -e 'rmarkdown::render("comments.md")' would probably work too but
render.r is shorter and little more powerful so I use it more often myself) on the input file
comments.md which also happens to (here sole) file being monitored.
And that is really all there is to it. I wanted / needed something like this a few months ago at work too, and may have used an
inotify-based tool there but cannot find any notes. Python has something similar via watchdog which is yet again more complicated / general.
It turns out that auto-processing is actually not that helpful as we often save before an expression is complete, leading to needless error messages. So at the end of the day, I often do something much simpler. My preferred editor has a standard interface to ‘building’: pressing
C-x c loads a command (it recalls) that defaults to
make -k (i.e.,
make with error skipping). Simply replacing that with
render.r comments.md (in this case) means we get an updated pdf file when we want with a simple customizable command / key-combination.
So in sum: it is worth customizing your environments, learning about what your OS may have, and looking beyond a single tool / editor / approach. Even dreams may come true …