I need to download two files with a single click of a button (instead of two buttons). This is easy to do indepedently; however, I'm finding it near impossible for this with only one button. In the app example below, write.csv(mtcarss_3(), file = file) gets executed inside the downloadHandler; however, not the write.csv(mtcarss_4(), file = file) part. Is it possible to do this in Shiny? If so, how? Thanks for your help
library(shiny)
library(dplyr)
ui <- fluidPage(
h3("mtcars df gear == 4"),
h3("mtcars df gear == 3"),
downloadButton("gears", "download both tables")
)
server <- function(input, output, session) {
rv = reactiveVal("gears_3")
mtcarss <- reactive(
mtcars
)
mtcarss_3 <- reactive({
req(mtcarss())
mtcarss() %>%
filter(gear == 3) %>%
pull(mpg)
})
mtcarss_4 <- reactive({
req(mtcarss())
mtcarss() %>%
filter(gear == 4) %>%
pull(mpg)
})
output$gears <- downloadHandler(
filename = function() {"gears_3.txt"},
content = function(file) {
write.csv(mtcarss_3(), file = file)
write.csv(mtcarss_4(), file = file)
}
)
}
shinyApp(ui, server)