Dynamic R Markdown Reports with Shiny

R
development
Author

Stefan Eng

Published

August 31, 2019

TL;DR here is an example application. See the explaination below.

library(shiny)

ui <- fluidPage(
  downloadButton('download')
)

server <- function(input, output) {
  output$download <- downloadHandler(
    filename = "listing.pdf",
    content = function(f) {
      # Create a new empty environment
      # This allows us to pass in only the relevant variables into the report
      e <- new.env()
      # Pass two data sets into our template
      e$datasets <- list(mtcars, iris)
      # Render the document
      rmarkdown::render('template.Rmd',
                        output_format = rmarkdown::pdf_document(),
                        output_file=f,
                        envir = e)
    }
  )
}

shinyApp(ui = ui, server = server)

A common issue I have run into is dynamically generating reports. In particular, during my summer at Novartis there was lots of demands for report listings that could be generated on the fly from a Shiny application. Using the great rmarkdown package makes it really easy to do this. My approach is to create a template R markdown file and dynamically generate it using rmarkdown::render in a download handler.

The full application is available here: https://github.com/stefaneng/Shiny-Dynamic-Report-Generation

We first need a R markdown template that we can use for the report generation. We want to print out some data set using the pander package to make nicer formatted tables.

---
title: "Example Template"
author: "Stefan Eng"
date: "8/7/2019"
output: pdf_document
---

```{r setup, include=FALSE}
library(knitr)
library(pander)
knitr::opts_chunk$set(echo = FALSE)
```

```{r, results='asis'}
panderOptions('knitr.auto.asis', FALSE)
for(d in datasets) {
  pander::pander(d, split.table=120)
}
```

To generate the report from Shiny, we use a downloadHandler and render the template using rmarkdown::render. This is called each time the user clicks the download button. In the full example on github, the user can select a data set to include in the report which is more realistic. The example given is the simplest to get you going on generating your own dynamic reports.

Reproducibility

─ Session info ───────────────────────────────────────────────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.2.1 (2022-06-23)
 os       macOS Catalina 10.15.7
 system   x86_64, darwin17.0
 ui       X11
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       America/Los_Angeles
 date     2022-12-11
 pandoc   2.19.2 @ /Applications/RStudio.app/Contents/MacOS/quarto/bin/tools/ (via rmarkdown)

─ Packages ───────────────────────────────────────────────────────────────────────────────────────────────────────────
 ! package     * version date (UTC) lib source
 P cli           3.4.1   2022-09-23 [?] CRAN (R 4.2.0)
 P digest        0.6.31  2022-12-11 [?] CRAN (R 4.2.1)
 P evaluate      0.18    2022-11-07 [?] CRAN (R 4.2.0)
 P fastmap       1.1.0   2021-01-25 [?] CRAN (R 4.2.0)
 P glue          1.6.2   2022-02-24 [?] CRAN (R 4.2.0)
 P here        * 1.0.1   2020-12-13 [?] CRAN (R 4.2.0)
 P htmltools     0.5.4   2022-12-07 [?] CRAN (R 4.2.0)
 P htmlwidgets   1.5.4   2021-09-08 [?] CRAN (R 4.2.0)
 P jsonlite      1.8.4   2022-12-06 [?] CRAN (R 4.2.1)
 P knitr         1.41    2022-11-18 [?] CRAN (R 4.2.1)
 P lifecycle     1.0.3   2022-10-07 [?] CRAN (R 4.2.0)
 P magrittr      2.0.3   2022-03-30 [?] CRAN (R 4.2.0)
   renv          0.16.0  2022-09-29 [1] CRAN (R 4.2.0)
 P rlang         1.0.6   2022-09-24 [?] CRAN (R 4.2.0)
 P rmarkdown     2.18    2022-11-09 [?] CRAN (R 4.2.0)
 P rprojroot     2.0.3   2022-04-02 [?] CRAN (R 4.2.0)
 P rstudioapi    0.14    2022-08-22 [?] CRAN (R 4.2.0)
 P sessioninfo * 1.2.2   2021-12-06 [?] CRAN (R 4.2.0)
 P stringi       1.7.8   2022-07-11 [?] CRAN (R 4.2.0)
 P stringr       1.5.0   2022-12-02 [?] CRAN (R 4.2.0)
 P vctrs         0.5.1   2022-11-16 [?] CRAN (R 4.2.0)
 P xfun          0.35    2022-11-16 [?] CRAN (R 4.2.0)
 P yaml          2.3.6   2022-10-18 [?] CRAN (R 4.2.0)

 [1] /Users/stefaneng/personal_devel/stefanengineering.comV3/renv/library/R-4.2/x86_64-apple-darwin17.0
 [2] /Users/stefaneng/personal_devel/stefanengineering.comV3/renv/sandbox/R-4.2/x86_64-apple-darwin17.0/84ba8b13

 P ── Loaded and on-disk path mismatch.

──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────