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:
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!