Converting Sweave LaTeX to knitr LaTeX: A case study

June 9, 2012
By

(This article was first published on Jeromy Anglim's Blog: Psychology and Statistics, and kindly contributed to R-bloggers)

The following post documents the steps I needed to take in order to convert a project using Sweave LaTeX into one using knitr LaTeX.

Additional Resources

It is fairly straightforward to convert a document from Sweave LaTeX to knitr LaTeX. Yihui Xie on the knitr website provides the following useful resources:

  • Transition to Sweave from knitr: This document describes knitr specifically from the perspective of what is the same as Sweave and what is different from Sweave.
  • knitr options: This includes discussion of the many R code chunk options in knitr. Many are the same as Sweave, but there are some new ones, and some modifications.
  • knitr minimal examples: These are useful for getting started with different types of knitr document including LaTeX.

My conversion from Sweave to knitr

The following documents the steps I needed to do in order to convert a journal article that was in Sweave LaTeX into a knitr LaTeX document. Most of this was documented in the above mentioned links on the knitr website, but there were still a few little surprises.

  • Rnw to tex conversion: Convert R CMD Sweave myfile.rnw to Rscript -e "library(knitr); knit('myfile.nw')" in makefile (see this SO question ).
  • global options: Replace \SweaveOpts{echo=FALSE} with \Sexpr{opts_chunk$set(echo=FALSE)}; This needed to appear before the first R code chunk in order to affect all code chunks in the file.
  • case on R code chunk options: Update true and false to TRUE and FALSE in r code chunk options.
  • results option: Update results=tex to results='asis' and in general ensure that text values in R code chunks are surrounded by quotation marks.
  • message option: I needed to prevent the display of messages when certain packages were loaded using \Sexpr{opts_chunk$set(message=FALSE}}.
    These messages did not previously display under sweave.
  • hiding output: I had some R code chunks with options print=FALSE, term=FALSE; I replaced this with results='hide'.
  • methods package: I had a densityplot() (i.e., a lattice plot) that didn't display properly. It instead showed an error: Error using packet 1 could not find function "hasArg"; apparently this is caused by the fact that the methods package doesn't load by default when using Rscript; thus I needed to put require(methods) in the first R code chunk.
  • Sweave.sty: I removed Sweave.sty from my project directory and removed the line \usepackage{Sweave} from my rnw file as both things are not needed in knitr.
  • caching: Although there are packages for enabling caching, I'd never adopted any of them. knitr makes caching very simple. I just added cache=TRUE to the global chunk options (i.e., \Sexpr{opts_chunk$set(echo=FALSE, message=FALSE, cache=TRUE)}. This reduced the time to build the PDF from around 5 seconds to 1 second. I'm also planning to incorporate some Bayesian analyses with JAGS and rjags, where I'm expecting analyses will take several minutes or longer to run. At that point, I'll really appreciate the speed benefits of caching.
  • to make or not to make: I had a custom makefile on the project that kept everything neat and tidy, copying source files into a build directory, running all necessary commands to convert from rnw to tex and then to pdf, and then opening the pdf in a viewer. This still works well. However, the default "Compile to PDF" option in RStudio was also quite good (after setting tools - options - Sweave - Weave Rnw files using knitr). In particular, I liked the synctex support for Sweave that allows you to move from a position in the source to the corresponding position in the PDF viewer. Also, RStudio in combination with knitr seems to do a reasonable job of keeping the main project directory tidy. A few auxiliary files are added, but not too many. I also appreciate the simplicity that a simple button brings to getting started with analyses. However, a makefile does make things more portable.

My main conclusion from this process is that converting an ongoing Sweave LaTeX document to knitr LaTeX is fairly straightforward, and there are a number of useful benefits that arise. In particular, I really appreciate simple caching and not having to worry about Sweave.sty. Great work Yihui Xie!

Additional Resources

To leave a comment for the author, please follow the link and comment on his blog: Jeromy Anglim's Blog: Psychology and Statistics.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.