1

I have asked a similar question to pass one output from a module to another module to generate a report.

However, I still cannot figure out how to export multiple outputs to include them in a downloadable report.

What I did is first to save the multiple outputs in a list, and then export them.

But I don't known how to pass them to the download module. In specific, I don't understand what does plotList = getPlot mean in download_server("download_ui_2", plotList = getPlot). I thought this is used to pass the multiple output from module getPlot to module download. But it did not work. I got the following error:

Error in download_server("download_ui_2", plotList = getPlot) : unused argument (plotList = getPlot)

Could someone help me out and explain me a bit of the above code?

Thanks a lot!

library(shiny)
library(ggplot2)
library(rmarkdown)
# Module 1,
plot_ui <- function(id) {
  ns <- NS(id)
  tagList(
    plotOutput(ns("plot1")),
    plotOutput(ns("plot2"))
  )
}

plot_server <- function(id) {
  moduleServer(id, function(input, output, session) {
    myplot1 <- reactive({
      ggplot(mtcars, aes(wt, mpg)) +
        geom_point()
    })
    myplot2 <- reactive({
      ggplot(mtcars, aes(wt, drat)) +
        geom_point()
    })
    output$plot1 <-renderPlot({
      myplot1()
    })
    output$plot2 <-renderPlot({
      myplot2()
    })

    plotList <- list(myplot1 = myplot1, myplot2 = myplot2) ## save in a list
    return(plotList)
  })
}

# Module 2
download_ui <- function(id) {
  ns <- NS(id)
  tagList(
    downloadButton(outputId = ns("report_button"),
                   label = "Generate report"
    )
  )
}

download_server <- function(id, myplot) {
  moduleServer(id, function(input, output, session){
    output$report_button<- downloadHandler(
      filename = "report.html",
      content = function(file) {
        tempReport <- file.path(tempdir(), "myRport.Rmd")
        file.copy("myReport.Rmd", tempReport, overwrite = TRUE)
        params <- list(plot1 = myplot1())
        rmarkdown::render(tempReport,
                          output_file = file,
                          params = params,
                          envir = new.env(parent = globalenv())
        )
      }
    )
  }
  )
}

# Application
library(shiny)
library(ggplot2)

app_ui <- function() {
  fluidPage(
    plot_ui("plot_ui_1"),
    download_ui("download_ui_2")
  )
}

app_server <- function(input, output, session) {
  getPlot <- plot_server("plot_ui_1")
  download_server("download_ui_2", plotList = getPlot)
}

shinyApp(app_ui, app_server)

Wang
  • 1,314
  • 14
  • 21
  • “It didn’t work” is not helpful. Please be more specific. In what way did it not work? Did you get an error message? If so what was it? Did you get a result that differed from your expectation? If so, what was the actual result and how was it different from your expectation? Also, your code is not reproducible, since we are missing your report template. – Limey Jan 04 '23 at 08:11
  • @Limey, Thanks Limey, I have edited my question to make it more specific. – Wang Jan 04 '23 at 08:14
  • 2
    Also, you pass your list to the download server as `myplot`, yet reference it as `myplot1`. – Limey Jan 04 '23 at 08:17
  • @Limey Thank you very much. It works now with your kind help. – Wang Jan 04 '23 at 08:28

1 Answers1

0

The code works now after help from @Limey.

library(shiny)
library(ggplot2)
library(rmarkdown)
# Module 1,
plot_ui <- function(id) {
  ns <- NS(id)
  tagList(
    plotOutput(ns("plot1")),
    plotOutput(ns("plot2"))
  )
}

plot_server <- function(id) {
  moduleServer(id, function(input, output, session) {
    myplot1 <- reactive({
      ggplot(mtcars, aes(wt, mpg)) +
        geom_point()
    })
    myplot2 <- reactive({
      ggplot(mtcars, aes(wt, drat)) +
        geom_point()
    })
    output$plot1 <-renderPlot({
      myplot1()
    })
    output$plot2 <-renderPlot({
      myplot2()
    })

    plotList <- list(myplot1 = myplot1, myplot2 = myplot2)
    return(plotList)
  })
}

# Module 2
download_ui <- function(id) {
  ns <- NS(id)
  tagList(
    downloadButton(outputId = ns("report_button"),
                   label = "Generate report"
    )
  )
}

download_server <- function(id, plotList) {
  moduleServer(id, function(input, output, session){
    output$report_button<- downloadHandler(
      filename = "report.html",
      content = function(file) {
        tempReport <- file.path(tempdir(), "myRport.Rmd")
        file.copy("myReport.Rmd", tempReport, overwrite = TRUE)
        params <- list(plot1 = plotList$myplot1(),
                       plot2 = plotList$myplot2()
                       )
        rmarkdown::render(tempReport,
                          output_file = file,
                          params = params,
                          envir = new.env(parent = globalenv())
        )
      }
    )
  }
  )
}

# Application
library(shiny)
library(ggplot2)

app_ui <- function() {
  fluidPage(
    plot_ui("plot_ui_1"),
    download_ui("download_ui_2")
  )
}

app_server <- function(input, output, session) {
  getPlot <- plot_server("plot_ui_1")
  download_server("download_ui_2", plotList = getPlot)
}

shinyApp(app_ui, app_server)

Wang
  • 1,314
  • 14
  • 21