0

I'm building a report using R Markdown and the output is a PDF document. The report summarizes some data - tables & figures - by various states. I want to the output to be formatted so that all of the tables/figures for each state appear on a different page. But the states included in the report is not fixed. Any ideas how to go about this?

Sample code below. In the example code, I'd want one page per species.

``` {r echo = FALSE}

for (i in temp_species) {
  
  iris %>%
    filter(Species == i) %>%
    ggplot() +
      geom_density(aes(Sepal.Length)) -> plot_1
  
  iris %>%
    filter(Species == i) %>%
    ggplot() +
      geom_density(aes(Sepal.Width)) -> plot_2
  
  print(plot_1)
  print(plot_2)
  
}

```
jlk199
  • 115
  • 5
  • 1
    Produce a child ("each repeating page") rmarkdown document (no yaml front matter needed). From within the main Rmd file, loop over what you need on each individual page, calling `knitr::knit_child("child.Rmd")`. Perhaps `out <- character(0); for (plt in plots) out <- c(out, knitr::knit_child("child.Rmd"))`. Then call `cat(out, collapse="\n")`. The child Rmd should likely have a LaTeX `\pagebreak` in it. – r2evans Apr 18 '23 at 19:35
  • You can move your loop to a .R document and use render() from rmarkdown to render each pdf from the .rmd file https://bookdown.org/yihui/rmarkdown-cookbook/rmarkdown-render.html – Susan Switzer Apr 18 '23 at 19:37
  • this [solution](https://stackoverflow.com/a/31831621/16421247) uses LaTeX commands (e.g. ``\pagebreak``) directly within code chunks. you have to specify within your chunk parameters ``results="asis"`` – nightstand Apr 18 '23 at 20:04

0 Answers0