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)