A knitr document contains sections of R and LaTeX. My boss wants to read the summary (in LaTeX), but doesn't want to read the R. However the R should be available in an appendix, so the code can be checked if needs be (see below). How to I make values and charts available to LaTeX (for the boss), before they have been created by R in the appendix?
\documentclass[12pt, a4paper]{article}
\begin{document}
\title{For Bosses and R Experts}
\author{Joe Collins}
\maketitle
\section{For the Boss}
The average is 3.3. Surely this should be calculated?
\appendix
\section{For The R Expert}
\subsection{Data}
<<data, echo=TRUE, results='markup'>>=
n = c(2, 3, 5)
s = c("One", "Two", "Three")
df = data.frame(n, s)
@
\subsection{Chart}
Show a chart.
<<chart, echo=TRUE, fig.height=3, fig.lp="chart">>=
barplot(df$n, names.arg=df$s)
@
\subsection{Statistics}
Calculate the mean.
<<statistics, echo=TRUE>>=
the.mean <- mean(df$n)
@
The arithmetic mean is \Sexpr{the.mean}.
\end{document}
I could reuse the chunk names like this (see below), but now all the R code is at the beginning of the document and separated from the surrounding discussion in the appendix. It is not an issue in this demo, but the actual document I am working on is huge.
\documentclass{article}
\begin{document}
\title{For Bosses and R Experts}
\author{Joe Collins}
\maketitle
<<data, echo=FALSE>>=
n = c(2, 3, 5)
s = c("One", "Two", "Three")
df = data.frame(n, s)
@
<<statistics, echo=FALSE>>=
the.mean <- mean(df$n)
@
\section{For the Boss}
The average is \Sexpr{the.mean}.
\appendix
\section{For The R Expert}
\subsection{Data}
<<data, eval=FALSE>>=
@
\subsection{Chart}
Show a chart.
<<chart, echo=TRUE, fig.height=3, fig.lp="chart">>=
barplot(df$n, names.arg=df$s)
@
\subsection{Statistics}
Calculate the mean.
<<statistics, eval=FALSE>>=
@
The arithmetic mean is \Sexpr{the.mean}.
\end{document}
Reusing the chunks in a slightly different way (thanks @george-dontas) gets me what I want. The calculated values before the R and the R with its discussion in the appendix.