I figured out a decent way to run knitr reports on different data sets easily. For my example instead of loading a new data set in each report I just use a different random number seed and print out some data. You’ll need the brew and knitr packages installed.

First you’ll need to generate your generic template report using ‘brew’ syntax. I have the following saved in a file called “template.Rnw”. Typically brew files are stored with a .brew file extension but that was causing RStudio to freak out and I didn’t get the nice knitr syntax highlighting so I went with a .Rnw extension.

\documentclass{article}
\begin{document}

<<loadingchunk, echo=FALSE>>=

## For our example the only thing I'm changing is
## the seed used
set.seed(<%= x %>)
mydata <- rnorm(5)
@

Blah blah blah.

Let's look at our data using a seed of <%= x %>

<<printingchunk>>=
print(mydata)
@

\end{document}

Notice that this is the report I want to generate for different data sets. In my case the different data sets are created with different input seeds but this could very easily be different file names directing to different data sets to load. My template assumes that the input will be stored in “x”. After running brew on this file anything between <% %> will be replaced with what is contained in the actual R object. So if I have an number stored in x then that is what will end up being the seed for the random number generator after calling brew(“template.Rnw”).

In a separate file I have the following:

# Load the necessary packages
library(brew)
library(tools)
library(knitr)

# Function to write report
# Note that since I used "x" in my template I need to use
# "x" as my input in create.report.
create.report <- function(x, prepend = "MyFile_"){
 rnw.file <- paste0(prepend, x, ".Rnw")
 # This generates the actual rnw file for different seeds
 brew('template.Rnw', rnw.file)
 knit(rnw.file)
 latex.file <- paste0(prepend, x, ".tex")
 texi2pdf(latex.file, clean = TRUE, quiet = TRUE)
 out.file <- paste0(prepend, x, ".pdf")
 return(out.file)
}

# This is the input that will change in each report
seeds <- c(42, 314159, 642)
# This is us generating the reports!
results <- sapply(seeds, create.report)

If you have both files in the working directory and run the second file you should end up with a pdf for each of the 3 seeds we used as input.

Like I mentioned you could easily replace the seeds with different data set names to do something more useful.