Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Markdown PDF, how to add page break after each iteration of for loop?

For example if my data frame was:

 exampledf <- data.frame(column = c("exampletext1", "exampletext2",  "exmapletext3"))

I would like the first page to have "exampletext1", the second page to have "exampletext2", etc.

\pagebreak works:

```{r, echo=FALSE}; exampledf[1,]```
   \pagebreak  
```{r, echo=FALSE} exampledf[2,]```

But my data frame is too large to make it practical.

I really need to loop through all my values:

for(i in 1:NROW(exampledf)) {
  single <- exampledf[i]
  strwrap(single, 70))
}

It's a weird question I realize.

like image 518
RyGuy Avatar asked Dec 28 '25 21:12

RyGuy


1 Answers

You can put \\newpage inside the cat function to add a page break after each iteration of the loop. The chunk also needs to have the parameter results="asis". For example, does something like this work for you:

```{r echo=FALSE, results="asis", warning=FALSE}
library(xtable)

exampledf <- data.frame(column = c("exampletext1", "exampletext2",  "exmapletext3"))

for (i in 1:nrow(exampledf)) {

  # Just added this as another way of displaying a row of data
  print(xtable(exampledf[i, , drop=FALSE]), comment=FALSE)

  print(strwrap(exampledf[i,], 70))

  cat("\n\\newpage\n")

}

```
like image 164
eipi10 Avatar answered Dec 31 '25 11:12

eipi10