summarize results dynamically produced by a template in R knitr
<p>I am writing a report using <code>knitr</code>, with a template that runs analyses
over multiple datasets (Section 1 of the MWE).</p>

<p>I can generate summaries of the results by assigning values to
variables that I then "stitch together" into tables (Section 2 of the
MWE). However, this approach is cumbersome and inflexible (e.g. lots
of typing to change what specific bits appear in the table).</p>

<p>How can I automate the production of the summary tables?</p>

<ul>
<li><p>Body of the report (MWE.Rnw):</p>

<pre><code>\documentclass{article}

\begin{document}

\tableofcontents

\newpage
\section{Run tests}

In this section we run the tests using a template.

&lt;&lt;run-all, include = FALSE&gt;&gt;=
library(knitr)

## set data names
data_names &lt;- LETTERS[1:3]

## initialize var for data
data_1 &lt;- NULL
data_2 &lt;- NULL

## initialize vars for chi-squared test results
cs_statistic &lt;- NULL                    # X-squared
cs_parameter &lt;- NULL                    # df
cs_p_value &lt;- NULL                      # p-value

## initialize vars for binomial test results
bt_p_value &lt;- NULL                      # p-value
bt_estimate &lt;- NULL                     # estimate
bt_ci_lower &lt;- NULL                     # conf. int. lower
bt_ci_upper &lt;- NULL                     # conf. int. upper

## run template
src = NULL
for (i in data_names) src = c(src, knit_expand('analysis-template.Rnw'))
@

\Sexpr{paste(knit(text = src), collapse = '\n')}

\newpage
\section{Summary}

In this section we summarise the results.

&lt;&lt;summary-cs&gt;&gt;=
tab &lt;- data.frame(data_1, data_2, round(cs_statistic, 3), cs_parameter, round(cs_p_value, 3), row.names = data_names)
colnames(tab) &lt;- c("var 1", "var 2", "X-squared", "d.f.", "p-value")

kable(tab, caption = "Summary results of $\\chi^2$ tests")
@ 

&lt;&lt;summary-bt&gt;&gt;=
tab &lt;- data.frame(data_1, data_2, round(bt_estimate, 3), round(bt_ci_lower, 3), round(bt_ci_upper, 3), round(bt_p_value, 3), row.names = data_names)
colnames(tab) &lt;- c("var 1", "var 2", "estimate", "95% conf. int. (lower)", "95% conf. int. (upper)", "p-value")

kable(tab, caption = "Summary results of binomial tests")
@ 

\end{document}
</code></pre></li>
<li><p>Template (analysis-template.Rnw) called by MWE.Rnw:</p>

<pre><code>\subsection{Analysis of data {{i}}}

&lt;&lt;analysis-{{i}}&gt;&gt;=
## generate data {{i}}
(data_{{i}} &lt;- sample(50:100, 2))

## run tests
(cs &lt;- chisq.test(data_{{i}}))
(bt &lt;- binom.test(data_{{i}}[1], sum(data_{{i}})))

## store results
data_1 &lt;- c(data_1, data_{{i}}[1])
data_2 &lt;- c(data_2, data_{{i}}[2])

cs_statistic &lt;- c(cs_statistic, cs$statistic)
cs_parameter &lt;- c(cs_parameter, cs$parameter)
cs_p_value &lt;- c(cs_p_value, cs$p.value)

bt_estimate &lt;- c(bt_estimate, bt$estimate)
bt_ci_lower &lt;- c(bt_ci_lower, bt$conf.int[1])
bt_ci_upper &lt;- c(bt_ci_upper, bt$conf.int[2])
bt_p_value &lt;- c(bt_p_value, bt$p.value)
@
</code></pre></li>
</ul>