title: Using loops with knitr to produce multiple pdf reports... need a little help to get me over the hump
body: <p>First of all, I must admit that I'm very new to knitr and the concept of reproducible analysis, but I can see its potential in improving my current workflow (which includes much copy-pasting into word docs).</p>
  
  <p>I often have to produce multiple reports by group (Hospital in this example) and within each hospital, there may be many different Wards that I'm reporting an outcome on.  Previously I ran all of my plots and analysis in R using loops, then the copy/pasting work commenced; however, after reading this post (<a href="https://stackoverflow.com/questions/8519128/can-sweave-produce-many-pdfs-automatically">Can Sweave produce many pdfs automatically?</a>), and it gave me hope that I may actually be able to skip many steps and go straight from R to report through Rnw/knitr.</p>
  
  <p>However, after giving it a try I see that there is something that isn't quite working out (as the R environment within the Rnw does not appear to recognize the looping variables I'm trying to pass to it??).   </p>
  
  <pre><code>   ##  make my data
  Hospital &lt;- c(rep("A", 20), rep("B", 20))
  Ward &lt;- rep(c(rep("ICU", 10), rep("Medicine", 10)), 2)
  Month &lt;- rep(seq(1:10), 4)
  Outcomes &lt;- rnorm(40, 20, 5)
  df &lt;- data.frame(Hospital, Ward, Month, Outcomes)
  
  
  ##  Here is my current work flow-- produce all plots, but export as png and cut/paste
  for(hosp in unique(df$Hospital)){
    subgroup &lt;- df[ df$Hospital == hosp,]
    for(ward in unique(subgroup$Ward)){
      subgroup2 &lt;- subgroup[subgroup$Ward == ward,]
      savename &lt;- paste(hosp, ward)
      plot(subgroup2$Month, subgroup2$Outcomes, type="o", main=paste("Trend plot for", savename))
    }
  }
  # followed by much copy/pasting
  
  
  ##  Here is what I'm trying to go for using knitr 
  library(knitr)
  for (hosp in unique(df$Hospital)){
    knit("C:file.path\\testing_loops.Rnw", output=paste('report_', Hospital, '.tex', sep=""))
  }
  
  ## With the following *Rnw file
  ## start *.Rnw Code
  \documentclass[10pt]{article}
  \usepackage[margin=1.15 in]{geometry}
  &lt;&lt;loaddata, echo=FALSE, message=FALSE&gt;&gt;=
    Hospital &lt;- c(rep("A", 20), rep("B", 20))
  Ward &lt;- rep(c(rep("ICU", 10), rep("Medicine", 10)), 2)
  Month &lt;- rep(seq(1:10), 4)
  Outcomes &lt;- rnorm(40, 20, 5)
  df &lt;- data.frame(Hospital, Ward, Month, Outcomes)
  subgroup &lt;- df[ df$Hospital == hosp,]
  @
  
  \begin{document}
  &lt;&lt;setup, echo=FALSE &gt;&gt;=
    opts_chunk$set(fig.path = paste("test", hosp , sep=""))
  @
  
  Some infomative text about hospital \Sexpr{hosp}
  
  &lt;&lt;plots, echo=FALSE &gt;&gt;=
    for(ward in unique(subgroup$Ward)){
      subgroup2 &lt;- subgroup[subgroup$Ward == ward,]
      #     subgroup2 &lt;- subgroup2[ order(subgroup2$Month),]
      savename &lt;- paste(hosp, ward)
      plot(subgroup2$Month, subgroup2$Outcomes, type="o", main=paste("Trend plot for", savename))
    }
  @
  \end{document}
  
  
  ##  To be then turned into pdf with this
  tools::texi2pdf("C:file.path\\report_A.tex", clean = TRUE, quiet = TRUE)
  </code></pre>
  
  <p>After trying to run my knit() code chunk I get this error:</p>
  
  <pre><code>Error in file(con, "w") : invalid 'description' argument
  </code></pre>
  
  <p>And when I look into the directory where the *.tex file was to be created, I can see the 2 pdf plots from hospital A were produced (none for B) and no hospital specific *.tex file to knit into a pdf.  Thanks in advance for any help you can offer!</p>