Knit from the command line

Posted on


Knitr is a great way to combine document markup (Latex, Markdown, HTML, etc.) with R code for data analysis and visualization. It pulls out the chunks of R code, runs them, and re-inserts the results into the document source (usually a .tex file), which can then be compiled as usual. Normally you would call it from an R console (or use something like RStudio), but what if you want to call it from the command line, like latex?  Here’s a little shell script that I use to automate the knitting of .Rnw files (combining R and Latex): knit.sh.

It call knit() inside R, then runs pdflatex on the resulting file. It is very simple to use (you must of course have the knitr package installed in R):
knit.sh awesomefile.Rnw

This would produce awesomefile.pdf (as well as the intermediate file awesomefile.tex, and the extracted R commands, awesomefile.R). You might even rename the script as knit and put it somewhere on your search path (maybe /usr/local/bin/) to be even more fancy.

Advertisements

Questions? Thoughts?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s