Adjust download button to download two tables

The code below generates two tables, Table1 and Table2. When I press the Download button, I can download Table 1 only. This is working fine, but I would like to download Table 2 as well. Download the two tables together if possible.

I tried doing the following: writexl::write_xlsx(data_subset(), data_subset2(), path = file), but it didn't work. Can you help me?

I inserted an executable code below for you to test.

library(shiny)
library(shinythemes)
library(dplyr)
library(writexl)


Test <- structure(list(date2 = structure(c(18808, 18808, 18809, 18810
), class = "Date"), Category = c("FDE", "ABC", "FDE", "ABC"), 
coef = c(4, 1, 6, 1)), row.names = c(NA, 4L), class = "data.frame")

ui <- fluidPage(
  
  shiny::navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
                    br(),
                    
                    
                    tabPanel("",
                             fluidPage(
                               fluidRow(
                                 br(), br(),
                                 column(4,
                                        wellPanel(
                                            uiOutput("daterange"),
                                            downloadButton("dl", "Download")
                                             )),
                                 
                                 column(8,
                                        tabsetPanel(
                                          tabPanel("Table1", dataTableOutput('table')),
                                          tabPanel("Table2", dataTableOutput('table2'))
                
                                               )))
                                                       ))))

server <- function(input, output,session) {
  
  data <- reactive(Test)
  
  output$daterange <- renderUI({
    dateRangeInput("daterange1", "Period you want to see:",
                   start = min(data()$date2),
                   end   = max(data()$date2))
  })
  
  data_subset <- reactive({
    req(input$daterange1)
    days <- seq(input$daterange1[1], input$daterange1[2], by = 'day')
    subset(data(), date2 %in% days)
  })
 
  output$table <- renderDataTable({
    data_subset()
  })
  
  
  data_subset2 <- reactive({
    req(input$daterange1)
    days <- seq(input$daterange1[1], input$daterange1[2], by = 'day')
    subset(data(), date2 %in% days)
  })
  
  output$table2 <- renderDataTable({
    data_subset2()
  })
  
  output$dl <- downloadHandler(
    filename = function() { "data.xlsx"},
    content = function(file) {
      writexl::write_xlsx(data_subset(), path = file)
    }
  )
}

shinyApp(ui = ui, server = server)

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.