Skip to content

Instantly share code, notes, and snippets.

@nanounanue
Forked from yihui/main.Rnw
Created June 21, 2012 18:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nanounanue/2967469 to your computer and use it in GitHub Desktop.
Save nanounanue/2967469 to your computer and use it in GitHub Desktop.
use knit() with a loop to write reports for subsets of data
\documentclass{article}
\begin{document}
<<run-all, results='hide', message=FALSE>>=
library(xtable)
out = NULL
for (g in unique(mtcars$gear)) {
out = c(out, knit_child('template.Rnw', sprintf('gear-%s.tex', g)))
}
<<write-results, echo=FALSE, results='asis'>>=
cat(out, sep = '\n')
@
\end{document}
<<include=FALSE, cache=FALSE>>=
# make sure figure paths do not collide
opts_chunk$set(fig.path = paste('figure/gear', g, sep = ''))
@
\section{Cars with \Sexpr{g} gears}
<<results='asis'>>=
df = subset(mtcars, gear == g, 1:5)
print(xtable(df), floating = FALSE)
@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment