I analyze measurements from many cities (hundreds), and need to create separate reports per city (in Adobe pdf-format).
My process is like this:
- First RStudio to prepare the data to be shown, saved in X.Rda.
- In X.Rnw (RStudio) I read X.Rda, select one city, and produce the tables and plots.
- In RStudio I press “Compile PDF” and the city-report X.pdf is produced.
- 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
You can use something like a
forloop 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 sessionThe code will be like (suppose
citiesis a character vector, and I use theknitrpackage as an example because you can specify the filename of the output):Inside
city_template.Rnw, you have a chunk likeThen 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. Inknitr, this can be done by like this: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
knitrlater (this is off-topic, so I won’t expand here).