How to pass tables and plots from different modules in Shiny app as parameters to R Markdown? #971
Answered
by
YonghuiDong
YonghuiDong
asked this question in
Q&A
-
Dear golem development team, In this Shiny app, the user can upload a file, and get the results as tables and plots. I want to download the results as HTML document. I tried to either pass a
Thanks a lot. Dong Below is my minimal example: # Module 1,
plot_ui <- function(id) {
ns <- NS(id)
tagList(
plotOutput(ns("plot1")),
plotOutput(ns("plot2"))
)
}
plot_server <- function(id, r) {
moduleServer(id, function(input, output, session) {
plot1 <- reactive({plot(x = mtcars$wt, y = mtcars$mpg)})
output$plot1 <-renderPlot({
plot1()
})
output$plot2 <- renderPlot({
r$scatterPlot <- plot(x = mtcars$wt, y = mtcars$mpg)
r$scatterPlot
})
}
)
}
# Module 2
choice_ui <- function(id) {
ns <- NS(id)
tagList(
sliderInput(inputId = ns("slider"),
label = "Slider",
min = 1,
max = 100,
value = 50),
downloadButton(outputId = ns("report_button"),
label = "Generate report"
)
)
}
choice_server <- function(id, r) {
moduleServer(id, function(input, output, session){
output$report_button<- downloadHandler(
filename = "report.html",
content = function(file) {
tempReport <- file.path(tempdir(), "myRport.Rmd")
file.copy("inst/app/www/myReport.Rmd", tempReport, overwrite = TRUE)
params <- list(n = input$slider,
plot1 = plot1(),
plot2 = r$scatterPlot
)
rmarkdown::render(tempReport,
output_file = file,
params = params,
envir = new.env(parent = globalenv())
)
}
)
}
)
}
# Application
library(shiny)
app_ui <- function() {
fluidPage(
plot_ui("plot_ui_1"),
choice_ui("choice_ui_2")
)
}
app_server <- function(input, output, session) {
r <- reactiveValues()
plot_server("plot_ui_1", r = r)
choice_server("choice_ui_2", r = r)
}
shinyApp(app_ui, app_server) |
Beta Was this translation helpful? Give feedback.
Answered by
YonghuiDong
Jan 5, 2023
Replies: 1 comment
-
I figured out the solution:
Here is a mini example. 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) |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
YonghuiDong
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I figured out the solution:
Here is a mini example.