How can I use values and charts in a knitr / LaTeX document before R has calculated them?

83 views Asked by At

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}
2

There are 2 answers

0
Joe On BEST ANSWER

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.

\documentclass{article}

\begin{document}
\title{For Bosses and R Experts}
\author{Joe Collins}
\maketitle

<<*, echo=FALSE, include=FALSE>>=
<<data>>
<<chart>>
<<statistics>>
@

\section{For the Boss}
The average is \Sexpr{the.mean}.

\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}
0
Joe On

I less tidy solution (which might be useful if there are a lot of chunks or only a few variables) might be to use a LaTeX support file.

\documentclass{article}

\IfFileExists{\jobname.var}%
{%
  \input{\jobname.var}%
}{}%
\newwrite\variablesfile
\immediate\openout\variablesfile=\jobname.var
\newcommand{\newvariable}[2]{%
  \immediate\write\variablesfile{
    \string\newcommand{\string #1}{#2}
  }
}%

\begin{document}
\title{For Bosses and R Experts}
\author{Joe Collins}
\maketitle

\section{For the Boss}
The average is \mean.

\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)
@
\newvariable{\mean}{\Sexpr{the.mean}}

The arithmetic mean is \mean.
\end{document}