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=...
)
- Why does Sweave not allow white space with reused code chunks, especially since the workaround seems very trivial to implement indeed?
- How can I work around this problem and indent chunks (or any Sweave code for that matter) as much as I like?