I am using rmarkdown, pandoc and knitr to create a pdf including chunks of r code. Within a code chunk I have a for loop which prints a number of graphs and some statistical output.
I would like to insert a page break into the loop (to appear in the pdf output). This page break would occur after each graph is printed, to ensure each graph is printed on one page and the statistical output on the next.
I have been unable to find a way of including a page break in my r code chunk. I have tried cat("\\newpage")
and cat("\\pagebreak")
in the hopes it would be recognized by pandoc but to no avail (it is just printed verbatim in the final pdf).
Suggestions appreciated. Here is the code I have so far:
```{r, echo =FALSE, message=FALSE, warning=FALSE, comment=NA, results='asis'} library("markdown") library("rmarkdown") library("knitr") library("ggplot2") for (v in Values){ # read in file testR <- read.csv(file.path, header=T) print(ggplot(testR, aes(x=Time, y=Value, color=Batch)) + geom_point(size = 3) + xlab ("Timepoint") + ylab (v) + scale_x_continuous(breaks=seq(0, 60, by=6)) + ggtitle(paste("Scatterplot of Batches for ", v, sep=""))) ggsave(paste(timestamp, "__", "Scatterplot of Batches for ", v, ".jpeg", sep = "")) cat("\\pagebreak") writeLines(v) writeLines("\n") writeLines("\n Test for homogenity of slopes \n") av1 <- aov(Value~Time*Batch, data=testR) print(summary(av1)) } ```
See below a reduced and reproducible example. The answer and some general remarks:
results='asis'
in the chunk options.\n
) after \\pagebreak
or else "ValueForV"
will be pasted directly after "\linebreak"
, which results in an Undefined control sequence
error.\newpage
and \pagebreak
are in a separate line by using linebreaks \n
before.Escape \newpage
and \pagebreak
(i.e., \\newpage
, \\pagebreak
).
--- title: "test" output: pdf_document --- ```{r, echo=FALSE, results='asis'} for (i in 1:3) { print(ggplot2::qplot(i, i+1)) cat("\n\n\\pagebreak\n") writeLines("ValueForV") } ```
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