1

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}
Joe
  • 91
  • 7
  • http://yihui.name/knitr/demo/reference/ – gd047 Jun 15 '15 at 12:46
  • possible duplicate of [Defer code to END of document in knitr](http://stackoverflow.com/questions/30242753/defer-code-to-end-of-document-in-knitr) – r.bot Jun 15 '15 at 12:46
  • Thanks. Reusing the chunks is a good idea (I hadn't considered). The generated document would be as I would wish, but the majority of the R code would then be at the beginning of the source document and separated from it's surrounding discussion in the appendix. This seems inconsistent with the literate programming paradigm, not to mention inconvenient. Or am I missing something? – Joe Jun 15 '15 at 13:59
  • This has been discussed before. Please see https://github.com/yihui/knitr/issues/868 for some ideas. – Yihui Xie Jun 15 '15 at 17:09

2 Answers2

0

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}
Joe
  • 91
  • 7
0

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}
Joe
  • 91
  • 7