Q: Create a multipage pdf file with knitr and split it in many files I am creating a report made by multiple pages which need to be different files. 
I made a script which create iteratively many pdf files from one rmd file. But the process is very long an oddly enough sometimes the process randomly blocks due to a permission error (only on windows, on my personal mac the same script gives no problem).
I noticed that the long part of the process is the creation of the pdf files, while the rendering of the .rmd itself is quite fast, even for long reports. So I thought of creating one long pdf file with all the reports in different pages and separate them afterwards.
So my questions are:


*

*How do I force the new page in latex?

*How do I split the pdf by page in R?


Thanks

A: 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}

