You probably got a solution for you, but I am facing the same problem and probably other people aswell. I am using a .Rnw file in combination with a .R file. By the way, here's the question that I created:
How to create multiple PDFs with different contents from a single data frame?
A new page in a .Rnw file can be forced with the following code: \newpage
or //
To split a data frame into multiple PDFs I've used the following for loop, but it is not working like intented. Keep this in mind! I've also tried to create global variables which are embedded in the .Rnw file. These variables are altered in the for loop then.
Here's an example:
for(i in 1:nrow(mtcars)) {
g_title <- rownames(mtcars)[i]
knit2pdf(input = "template.Rnw", output = paste0("output\\", g_title, ".pdf"), quiet = TRUE)
}
Edit
I solved the problem. Actually it was pretty simple, because you can just use functions/methods and variables globally. So, you can define a variable and just embed it into your Sweave file (.Rnw).
As you can see in this example, I used the names of the cars, coming from the mtcars data field, to create multiple PDFs. I am just creating multiple PDFs in a for loop where I alter the variable g_title
which is then used in the Sweave file.
main.R
for(i in 1:nrow(mtcars)) {
g_title <- rownames(mtcars)[i]
knit2pdf(input = "main.Rnw",
output = paste0("output\\", g_title, ".pdf"),
quiet = FALSE,
envir = parent.frame())
}
template.Rnw
\documentclass{article}
\usepackage[ngerman]{babel}
\begin{document}
\begin{titlepage}
Titlepage
\end{titlepage}
\tableofcontents
\newpage
\section{Topic 1}
\newpage
\section{Topic 2}
\end{document}