0

I have generated a shinydashboard app to allow easy visualization of data in a list of dataframes in different tabs (e.g. datatable with a ggplot in one tab, summary data table in another). This is working great, but now I want to provide an "generate report pdf" option so the data can be saved for easy viewing in the future.

I want to output a pdf which has a combination of data tables and ggplot2 plots in the output, similar to this:

enter image description here

I would like one page per dataframe in my list, complete with a title row that states the name of the dataframe. This is probably going to require manual manipulation of plots and tables, but I haven't been able to figure out how to incorporate this level of formatting within a for loop (which is how I have been doing this). This may not be an appropriate way to approach the problem.

Here's a reproducible example of something that works, but the formatting is not as I desire.

---
title: "Multiple Plot pdf Report"
date: "`r format(Sys.Date(), '%B %d, %Y')`"
output: 
  pdf_document:
    keep_tex: FALSE
---

```{r, include=FALSE}
library(kableExtra)
library(ggplot2)
library(gridExtra)
```

```{r echo=FALSE, results='asis', fig.height=3, fig.width=3}
alldat1 <- mtcars[1:10, 1:5]
alldat2 <- mtcars[11:20, 1:5]
alldat.list <- list(alldat1, alldat2)
names(alldat.list) <- c("One", "Two")

for(i in 1:length(alldat.list)){

  print(kable(alldat.list[[i]]))

  print(
    ggplot(alldat.list[[i]], aes(x = mpg, y = cyl)) +
          geom_point(shape = 1) +    
          geom_smooth(method = lm) +   
          ggtitle(paste("Mpg vs Cyl: ", names(alldat.list[i])))
  )
  print(
    ggplot(alldat.list[[i]], aes(x = disp, y = hp)) +
              geom_point(shape = 1) +    
              geom_smooth(method = lm) +   
              ggtitle(paste("Disp vs HP: ", names(alldat.list[i])))
  )

 cat("\n\n\\pagebreak\n")
}

```

Help is much appreciated!

Dorton
  • 185
  • 1
  • 2
  • 11

1 Answers1

0

Look into using the tabset function to separate your reports on different pages

---

title: "Test"

output: html_document

---

## Try {.tabset}

### Tryto
```{r}
"page1"
```

### Work
```{r}
"page2"
```

Daniel_j_iii
  • 3,041
  • 2
  • 11
  • 27
  • For my real dataset, I have a list of dataframes of variable length and I am printing this to a pdf using latex formatting wherever possible, so I would like to avoid manually designing a page for every df every time I generate a report. I have found this https://stackoverflow.com/questions/34808612/how-make-2-column-layout-in-r-markdown-when-rendering-pdf which will allow me to generate two columns, and then populate them with plots/tables, but I'm not sure if I can generate one page per df with this formatting applied. – Dorton Apr 03 '20 at 23:14