3 min read

Dynamic R Markdown Reports with Shiny

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 3.6.3 (2020-02-29)
 os       macOS Mojave 10.14.6        
 system   x86_64, darwin15.6.0        
 ui       X11                         
 language (EN)                        
 collate  en_US.UTF-8                 
 ctype    en_US.UTF-8                 
 tz       Europe/Stockholm            
 date     2020-05-11                  

─ Packages ───────────────────────────────────────────────────────────────────────────────────────────────────────────
 package     * version date       lib source        
 assertthat    0.2.1   2019-03-21 [1] CRAN (R 3.6.0)
 backports     1.1.5   2019-10-02 [1] CRAN (R 3.6.0)
 blogdown      0.13    2019-06-11 [1] CRAN (R 3.6.0)
 bookdown      0.11    2019-05-28 [1] CRAN (R 3.6.0)
 cli           2.0.2   2020-02-28 [1] CRAN (R 3.6.0)
 crayon        1.3.4   2017-09-16 [1] CRAN (R 3.6.0)
 digest        0.6.25  2020-02-23 [1] CRAN (R 3.6.0)
 evaluate      0.14    2019-05-28 [1] CRAN (R 3.6.0)
 fansi         0.4.1   2020-01-08 [1] CRAN (R 3.6.0)
 glue          1.3.2   2020-03-12 [1] CRAN (R 3.6.0)
 here        * 0.1     2017-05-28 [1] CRAN (R 3.6.0)
 htmltools     0.4.0   2019-10-04 [1] CRAN (R 3.6.0)
 knitr         1.28    2020-02-06 [1] CRAN (R 3.6.0)
 magrittr      1.5     2014-11-22 [1] CRAN (R 3.6.0)
 Rcpp          1.0.3   2019-11-08 [1] CRAN (R 3.6.0)
 rlang         0.4.5   2020-03-01 [1] CRAN (R 3.6.0)
 rmarkdown     2.1     2020-01-20 [1] CRAN (R 3.6.0)
 rprojroot     1.3-2   2018-01-03 [1] CRAN (R 3.6.0)
 sessioninfo * 1.1.1   2018-11-05 [1] CRAN (R 3.6.0)
 stringi       1.4.6   2020-02-17 [1] CRAN (R 3.6.0)
 stringr       1.4.0   2019-02-10 [1] CRAN (R 3.6.0)
 withr         2.1.2   2018-03-15 [1] CRAN (R 3.6.0)
 xfun          0.12    2020-01-13 [1] CRAN (R 3.6.0)
 yaml          2.2.1   2020-02-01 [1] CRAN (R 3.6.0)

[1] /Library/Frameworks/R.framework/Versions/3.6/Resources/library