Andrei T
Andrei T

Reputation: 103

Add multiple outputs from shiny to html markdown

I created a shiny app in which I have multiple outputs (tables, graphs and text) based on user selected parameters. I would like to download the outputs in a HTML document. I am able to do this using the small example on https://shiny.rstudio.com/articles/generating-reports.html but I cannot seem to figure out how to use multiple outputs in the markdown file.

I looked around quite a bit and although there are some examples out there I still cannot seem to figure it out. Probably it's just me being inexperienced. The code I modified (poorly) bellow gives outputs test.text, test.text2 then test.text again.

I would like to be able to add multiple output values that will subsequently be used in the markdown. I am using all my outputs in reactive functions as I noticed that I cannot use output$ in downloadHandler

This is the code I am trying to use in downloadHandler

 test.text <- reactive({input$gendertext}) #input text written in boxes

 test.text2 <- reactive({input$agetext})

output$report <- downloadHandler(
    filename = "report.html",
    content = function(file) {
      tempReport <- file.path(tempdir(), "report.Rmd")
      file.copy("report.Rmd", tempReport, overwrite = TRUE)

      params <- list(n = test.text())
      params2 <- list(n = test.text2())

      rmarkdown::render(tempReport, output_file = file,
                        params = c(params,params2),
                        envir = new.env(parent = globalenv()))
    }
  )

As I have multiple outputs (ggplots, tables and text) I would like to be able to use test.text, test.text2, plot1...plotn etc. in markdonw. separately.

e.g.

---
title: "Dynamic report"
output: html_document
params:
  n: NA
---
```{r}
test.text
plot1
``

```{r}
test.text2
plot2
``

If there is an easier way to download an Html/pdf file from a shinyUI that would be amazing!

Thank you!

Upvotes: 2

Views: 846

Answers (2)

Andrei T
Andrei T

Reputation: 103

I figured it out! Thanks @Ruben Kazumov for the help.

In order to be able to add plots to a markdown, or any other thing that you might have inside an output$ you first need to wrap your plot in a reactive function.

e.g.

plot <- reactive({your plot(can be ggplot2 or whatever)}

You can render this in your application by using

output$plottorender <- renderPlot({plot()})

And lastly you can use the plot() you just created in a markdown!

 output$report <- downloadHandler(
    filename = "report.html",
    content = function(file) {
      tempReport <- file.path(tempdir(), "report.Rmd")
      file.copy("report.Rmd", tempReport, overwrite = TRUE)`

      # one list() container object for all the parameters
      # all the objects have unique names (keys)
      `params <- list(text1 = input$test.text1,
                     text2 = input$test.text2,
                     plot1 = plot(),
                     plot2 = plot())) # ... whatever you have to send to the report

      rmarkdown::render(tempReport, output_file = file,
                        params = params,
                        envir = new.env(parent = globalenv()))
    })

Upvotes: 3

Ruben Kazumov
Ruben Kazumov

Reputation: 3872

output$report <- downloadHandler(
    filename = "report.html",
    content = function(file) {
      tempReport <- file.path(tempdir(), "report.Rmd")
      file.copy("report.Rmd", tempReport, overwrite = TRUE)

      # one list() container object for all the parameters
      # all the objects have unique names (keys)
      params <- list(text1 = input$test.text1,
                     text2 = input$test.text2,
                     plot1 = output$plot1,
                     plot2 = output$plot2)) # ... whatever you have to send to the report

      rmarkdown::render(tempReport, output_file = file,
                        params = params,
                        envir = new.env(parent = globalenv()))
    }
  )

Then in report:

```{r}
params$text1 # inside the report, you should call the object by its key
params$plot1
``

```{r}
params$text2
params$plot2
``

Upvotes: 0

Related Questions