21

I analyze measurements from many cities (hundreds), and need to create separate reports per city (in Adobe pdf-format).

My process is like this:

  1. First RStudio to prepare the data to be shown, saved in X.Rda.
  2. In X.Rnw (RStudio) I read X.Rda, select one city, and produce the tables and plots.
  3. In RStudio I press "Compile PDF" and the city-report X.pdf is produced.
  4. I go to step 2, choose another city, and so on.

This is very tedious, and looks perfect for a for-loop per city, but how can it be done?

Thank you r-contributors!

/Chris

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
Chris
  • 2,256
  • 1
  • 19
  • 41
  • 4
    This can be done with a combination of `brew` and `sweave`. take a look at this example here http://stackoverflow.com/questions/8434371/run-sweave-or-knitr-with-objects-from-existing-r-session/8437769#8437769. Alternately, you can write a function that reads a `city` and compiles doc to pdf, and loop through cities. – Ramnath Dec 15 '11 at 12:38
  • 2
    Need to expand this a bit. How does your .Rnw select a city? What operating system are you using (because this can be easily scripted in Linux)? Or you can use the Sweave(file) function in R, but we need to know how to make the .Rnw use a specific set of data. It might be that you need to use 'brew' to make a specific .Rnw from a template. And then the next step is making the PDF - ?Sweave suggests texi2dvi from the tools package... – Spacedman Dec 15 '11 at 12:39

2 Answers2

27

You can use something like a for loop with a global variable changing, which controls which city you want to weave into the report; see the other post Run Sweave or knitr with objects from existing R session

The code will be like (suppose cities is a character vector, and I use the knitr package as an example because you can specify the filename of the output):

for (city in cities) {
   knit('city_template.Rnw', output = paste('report_', city, '.tex', sep = ''))
}

Inside city_template.Rnw, you have a chunk like

<<do-my-job>>=
make_plot(city, ...)
whatever(city, ...)
@

Then you will get a series of tex files named by the cities, and the rest of your job is to compile them to PDF (not possible for RStudio to compile multiple tex files, AFAIK, but it is trivial to do it in command line or in R with texi2dvi()).

There is one thing you need to be careful -- you have to use a different figure prefix (the option fig.path) for each output file, otherwise different cities can override each other's figure output. In knitr, this can be done by like this:

<<setup, echo=FALSE>>=
opts_chunk$set(fig.path = paste('my-prefix-', city, sep = ''))
@

I believe this should be safe to produce many reports with a loop.

BTW, you can certainly achieve the same goal with Sweave; perhaps you will know why I developed knitr later (this is off-topic, so I won't expand here).

Community
  • 1
  • 1
Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
0
  • Do that in R environnent.
  • Put all the output for the specific city in a list
  • Write your RnW with chunks just callings the outputs
  • Save your R image in the same folder as your Sweave report and set wd to this same folder
  • Sweave("yourfile.RnW", output = "yourcity.tex")
  • Use texi2dvi to compile your tex file from R

If you have many cities, you should put that into a function.

PerrySun
  • 187
  • 1
  • 1
  • 8