Gregor Thomas
Gregor Thomas

Reputation: 145775

Generating knitr reports

I'm pretty new to knitr, but I've written a script that generates a report for a county. One of the first lines in the first code chunk is display_county <- "King", and it queries a database to make all sorts of nice things about King County. Now I want to create reports for every county in my state. The only line in the script that needs be changed is the definition of display_county.

I know the brew packages is set up for stuff like this, and I know there's overlap between brew and knitr, but I don't know what I should be using.

This answer using Brew and Sweave would work with minor modifications, but is there a nice knitr way to bypass brew?

Upvotes: 10

Views: 509

Answers (1)

JAponte
JAponte

Reputation: 1538

If I understand correctly, you are going to use the same Rnw file for each county, so only the variable display_county will be different for each county. I would first make the call to the database to get all the names of counties and store them in a vector (say... myCounties). After that, your reports can be generated with a script containing the following:

for(dc in myCounties)  {
  knit2pdf(input='county_report.Rnw', output=paste0(dc, '_county_report.pdf'))
}

To handle errors more effectively, you can also wrap the knit2pdf call on a tryCatch statement:

for(dc in myCounties)  {
  tryCatch(knit2pdf(input='county_report.Rnw', output=paste0(dc, '_county_report.pdf')))
}

Upvotes: 5

Related Questions