Search code examples
parametersshinyreactiveflexdashboard

Pass reactives to flexdashboard


I have a large Shiny app, and I would like to provide the possibility to export a flexdashboard report. Thus, I added a downloadHandler which passes certain params to the flexdashboard. The flexdashboard itself should work without being deployed on a server, thus I do not use the runtime: shiny option.

How can I pass reactive values to the flexdashboard? If I do something like params <- list(y = test()), the report creation breaks.

Relevant part of server.R:

test <- reactive({
    [[SOME STUFF]]
})

output$report <- downloadHandler(
  # For PDF output, change this to "report.pdf"
  filename = "report.html",
  content = function(file) {

    # Copy the report file to a temporary directory before processing it, in
    # case we don't have write permissions to the current working dir (which
    # can happen when deployed).
    tempReport <- file.path(tempdir(), "report.Rmd")
    file.copy("report.Rmd", tempReport, overwrite = TRUE)

    # Set up parameters to pass to Rmd document
    params <- list(y = test())

    # Knit the document, passing in the `params` list, and eval it in a
    # child of the global environment (this isolates the code in the document
    # from the code in this app).
    rmarkdown::render(tempReport, output_file = file,
                      params = params,
                      envir = new.env(parent = globalenv())
    )
  }
)

Relevant parts of report.Rmd:

---
title: "Row Orientation"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
params:
  y: NA
---

```{r setup, include=FALSE}
library(flexdashboard)
library(ggiraph)
library(ggplot2)
library(magrittr)
```
Row
-------------------------------------

### Chart 1

```{r}
ggplot(params$y)
```

Solution

  • I figured out what was the issue: The reactive I wanted to pass to flexdashboard was not properly created at the time of report creation, and this caused the report creation to break... Passing reactives works exactly the same way as reporting data.frames: params <- list(y = test())