Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Great question! This works for me with the other bits you've supplied in your question. Note that I've replaced your <code>hosp</code> with just <code>x</code>. I've called your <code>Rnw</code> file <code>test.rnw</code></p> <pre><code># input 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) # generate the tex files, one for each hospital in df library(knitr) lapply(unique(df$Hospital), function(x) knit("C:\\emacs\\test.rnw", output=paste('report_', x, '.tex', sep=""))) # generate PDFs from the tex files, one for each hospital in df lapply(unique(df$Hospital), function(x) tools::texi2pdf(paste0("C:\\emacs\\", paste0('report_', x, '.tex')), clean = TRUE, quiet = TRUE)) </code></pre> <p>I've replaced your loops with<code>lapply</code> and anonymous functions, which often seem to be considered more <code>R</code>-ish.</p> <p>Here you can see where I replaced the <code>hosp</code> with <code>x</code> in the <code>rnw</code> file:</p> <pre><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 == x,] @ \begin{document} &lt;&lt;setup, echo=FALSE &gt;&gt;= opts_chunk$set(fig.path = paste("test", x , sep="")) @ Some informative text about hospital \Sexpr{x} &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(x, ward) plot(subgroup2$Month, subgroup2$Outcomes, type="o", main=paste("Trend plot for", savename)) } @ \end{document} </code></pre> <p>The result is two tex files (report_A.tex, report_B.tex), four PDFs for the figures (A1, A2, B1, B2) and two PDFs for the reports (report_A.pdf, report_B.pdf), each with their figures in them. Is that what you were after?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload