I'm starting to learn R and Sweave and have spent the last couple of hours trying to figure out a problem that, according to my Google-fu, seems to be rather unknown or uncommon (as I found only two references to the problem).
Namely, when re-using a code chunk in Sweave, it seems you are not allowed any preceding whitespace. For example:
\documentclass{article}
\begin{document}
<<label=fig1boxplot, include=FALSE>>=
boxplot(mpg~cyl,data=mtcars)
@
\begin{figure}
\centering
<<label=fig1, fig=TRUE, echo=FALSE>>=
<<fig1boxplot>>
@
\end{figure}
\end{document}
will not work as expected if there is any white space in front of any line beginning with <<
(errors vary depending on which line is indented). I found a work-around on the R developer mailing list that lets me indent the line where the chunk is reused (<<figboxplot>>
) but still not any other chunk (<<label=...
)
The short answer is to use knitr
instead, which allows indentation of code chunks.
For your question on Sweave, the answer is also short: it is by design (at least for now). My personal experience is that it is difficult to make it happen even if it is trivial in terms of implementation efforts as you said. That is a good reason for R add-on packages.