I'd like to generate a large number of reports. For simplicity, let's say I want to create 5 small pdf documents with only a simple title that cycles through a vector of names.
\documentclass[12pt]{article}
\newcommand{\dsfrac}[2]{\frac{\displaystyle #1}{\displaystyle #2}}
\author{Me}
\title{\Sexpr{print(namelist)}}
\maketitle
\end{document}
How would I go about cycling through the generation of these reports for:
namelist <- c("Tom","Dick","Harry","John","Jacob")
Thanks in advance!
PS: Bonus points for showing me how to define the name of the resulting PDF documents.
You can just call Sweave
in a loop, as follows.
# Create the template file, "test.Rnw"
template <- "test.Rnw"
cat("
\\documentclass{article}
\\title{\\Sexpr{namelist[i]}}
\\begin{document}
\\maketitle
\\end{document}
", file=template)
# Parameters
namelist <- c("Tom","Dick","Harry","John","Jacob")
# Main loop: just compile the file,
# it will use the current value of the loop variable "i".
for(i in 1:length(namelist)) {
Rnw_file <- paste("test_", i, ".Rnw", sep="")
TeX_file <- paste("test_", i, ".tex", sep="")
file.copy(template, Rnw_file)
Sweave(Rnw_file)
system(paste("pdflatex --interaction=nonstopmode", TeX_file))
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With