Robin Yeldell
Robin Yeldell

Reputation: 31

R markdown Page Breaks from Function

I am trying to break to a new page from a R markdown document that is calling a function to produce a set of graphics for each set of data (400+ pages in some cases). I am using LaTeX.

Any ideas? Here is a simple example that demonstrates some my challenge though it doesn't use a function to produce the output.

---
title: "Test Page Break"
output: pdf_document
---


```{r}
summary(cars)
cat("\\pagebreak")
print(cars)
```

Update #1:
My example didn't demostrate the use of a function within the code chunk. Here is a better example.

---
title: "Test Page Break"
output: pdf_document
---


```{r}
pr_w_pagebreak <- function() {
print(summary(cars))
cat("\\pagebreak")
print(cars)
}
pr_w_pagebreak()
```

Upvotes: 3

Views: 1442

Answers (1)

Benjamin
Benjamin

Reputation: 17279

The trouble you'll have with your chunks is that you want some results to appear as 'markup' and some to appear 'asis'. If you want to get the page break with the single block as you have it, you will need to do:

```{r, echo=-2}
summary(cars)
knitr::asis_output("\\pagebreak")
print(cars)
```

Otherwise, you'll have to do

```{r}
summary(cars)
```

\pagebreak

```{r}
print(cars)
```

And as Floo0 points out, you can get LaTeX code from the R chunks if you use results = 'asis', but you have to be careful because the following will not give you what you want.

```{r, results='asis'}
summary(cars)
cat("\\pagebreak")
print(cars)
````

Upvotes: 2

Related Questions