The following post documents the steps I needed to take in order to convert aproject using Sweave LaTeX into one using knitr LaTeX.
Additional Resources
It is fairly straightforward to convert a document from Sweave LaTeX to knitrLaTeX. Yihui Xie on the knitr website provides thefollowing useful resources:
- Transition to Sweave from knitr: Thisdocument describes knitr specifically from the perspective of what is the sameas Sweave and what is different from Sweave.
- knitr options: This includes discussion ofthe many R code chunk options in knitr. Many are the same as Sweave, but thereare some new ones, and some modifications.
- knitr minimal examples: These areuseful for getting started with different types of knitr document includingLaTeX.
My conversion from Sweave to knitr
The following documents the steps I needed to do in order to convert a journalarticle 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.rnwtoRscript -e"library(knitr); knit('myfile.nw')"in makefile (see this SOquestion ). - global options: Replace
\SweaveOpts{echo=FALSE}with\Sexpr{opts_chunk$set(echo=FALSE)}; This needed to appear before the firstR code chunk in order to affect all code chunks in the file. - case on R code chunk options: Update
trueandfalsetoTRUEandFALSEin r code chunk options. - results option: Update
results=textoresults='asis'and in general ensure that textvalues in R code chunks are surrounded by quotation marks. - message option: I needed to prevent the display of messages when certainpackages 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 withresults='hide'. - methods package: I had a
densityplot()(i.e., a lattice plot) thatdidn't display properly. It instead showed an error:Error using packet 1could not find function "hasArg"; apparently this is caused by the fact thatthe methods package doesn't load by default when usingRscript; thus Ineeded to putrequire(methods)in the first R code chunk. - Sweave.sty: I removed
Sweave.styfrom my project directory and removedthe line\usepackage{Sweave}from my rnw file as both things are not neededin knitr. - caching: Although there are packages for enabling caching, I'd neveradopted any of them. knitr makes caching very simple. I just added
cache=TRUEto the global chunk options (i.e.,\Sexpr{opts_chunk$set(echo=FALSE, message=FALSE, cache=TRUE)}. This reducedthe time to build the PDF from around 5 seconds to 1 second. I'm also planningto incorporate some Bayesian analyses with JAGS and rjags, where I'm expectinganalyses will take several minutes or longer to run. At that point, I'llreally appreciate the speed benefits of caching. - to make or not to make: I had a custom makefile on the project that kepteverything neat and tidy, copying source files into a build directory, runningall necessary commands to convert from rnw to tex and then to pdf, and thenopening 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 thesynctex support for Sweave that allows you to move from a position in the sourceto the corresponding position in the PDF viewer. Also, RStudio in combinationwith knitr seems to do a reasonable job of keeping the main project directorytidy. A few auxiliary files are added, but not too many. I also appreciate thesimplicity 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 LaTeXdocument to knitr LaTeX is fairly straightforward, and there are a number ofuseful benefits that arise. In particular, I really appreciate simple cachingand not having to worry about Sweave.sty. Great work Yihui Xie!
Additional Resources
- RSS Subscription options
- Convert Sweave LaTEx to knitr RMarkdown
- Getting started with R Markdown
- Getting started with R
- R Videos
- Sweave andmakefiles
Hiç yorum yok:
Yorum Gönder