2

(I expect the answer is straightforward, but haven't been able to figure it out yet)

Every week I need to create a series of reports in PDFs, generated through knitr (using latex). Every report is identical in structure (the names and number of variables varies, but how they are processed is similar). What I want to be able to do is to create a function that something works like this:

createReport <- function(xmlfile, Rnwfile) {
  ff <- parse xmlfile
  extract relevant data from ff
  calculate stats and generate tables and figures
  call knitr such that the correct variable names, stats, etc are fed into the Rnw file
  knit into a pdf file
  save the pdf file on disc
}

(It would also be fine to calculate all the stats etc inside the Rnw-file.)

This way I can simply call createReport(datafile.xml, report.Rnw) for a given datafile.xml, and the appropriate PDF will be generated. How can this be done?

(NOTE: my question is about how to call knitr in a loop/from a function such that it will process the correct files and correct variables. I do already have a working latex/knitr script for a given dataset, but that has the name of the particular xmlfile and its variables hard coded inside it)

Peter Verbeet
  • 1,786
  • 2
  • 13
  • 29
  • Something along the lines of this? http://yihui.name/knitr/demo/externalization/ – Roman Luštrik Aug 20 '14 at 20:09
  • Thanks, I had seen that. There are two issues that this does not solve: 1 the filename is still hard coded inside the script and would have to be manually altered for every file I want to create a report for. 2. several of my variables each generates about 4 pages of output, this includes tables and graphics generated by R and latex-generated text in between. Since the number of variables differs between datafiles, I need to loop through the scripts AND part of the latex code, for each variable. The externalization does not seem to make that possible, if I am not mistaken. – Peter Verbeet Aug 20 '14 at 21:37
  • Sorry for before. Did not understand the question. [This](http://stackoverflow.com/questions/15396755/using-loops-with-knitr-to-produce-multiple-pdf-reports-need-a-little-help-to) question seems similar. May offer some clues. – pbible Aug 21 '14 at 00:21
  • Thanks, this does indeed offer several useful clues. – Peter Verbeet Aug 21 '14 at 12:41

0 Answers0