R + Sweave is powerful but crufty. The equally powerful and more modern combination is R and knitr.
Sweave was not so much designed as constructed, and it shows. Knitr expressly tries to do the same things as Sweave, but
- Its chunk options are grouped and tidied and generally better organized (and less inclined to touch each other) than Sweave’s options.
- It allows any input language (e.g. R, Python and Awk) and any output markup languages (provided: LaTeX, HTML, Markdown and reStructuredText). (I'm writing a ConTeXt renderer myself, but it's a side project and not done yet.)
- Things like caching chunks and pretty-printing code are built-in.
This is an example knitr + LaTeX document, taken from the knitr minimal examples:
\documentclass{article}
\usepackage{graphicx}
%% begin.rcode setup, include=FALSE
% opts_chunk$set(fig.path='figure/latex-', cache.path='cache/latex-')
%% end.rcode
\begin{document}
Boring stuff as usual:
%% a chunk with default options
%% begin.rcode
% 1+1
%
% x=rnorm(5)
%% end.rcode
Now we know the first element of x is \rinline{x[1]}. And we also
know the 26 letters are \rinline{LETTERS}.
How about figures? Let's use the Cairo PDF device (assumes R $\geq$ 2.14.0).
%% begin.rcode cairo-scatter, fig.width=5, fig.height=5, out.width='.8\\textwidth'
% plot(cars) # a scatter plot
%% end.rcode
\end{document}
You would compile this document as follows (after adding the provided knit
script to your path):
knit mydocument.Rtex # compile to tex
# or
knit --pdf mydocument.Rtex # compile to pdf
As you see, code chunks are kept in comments, so that even if you don't have knitr
the document is still a valid LaTeX file. If you prefer the Sweave `<>...@ style of mixing LaTeX and R, save your file as .Rnw, instead, and write your chunks like so:
<<cairo-scatter, fig.width=5, fig.height=5, out.width='.8\\textwidth'>>=
... chunk contents...
@
In conclusion, this is why you want to use (a) literate reporting as (b) implemented by knitr:
- The programming language gets first pass, which gives you lots of power
- Your document is mostly Markdown/LaTeX/other markup language, which is way nicer for reading/writing documents.
- R’s knit package implements this in a nicer way than R’s Sweave package.
- There must be similar packages in other languages, but I don't know any (barring the ones aimed more at literate source code).