I have the shiny app below in which I have 2 reactive dataframes. I want when I click download to download one excel file with both dataframes in 2 different tabs with their names if possible.
library(shiny)
library(openxlsx)
ui <- fluidPage(
titlePanel("Download Data Tables as Excel"),
sidebarLayout(
sidebarPanel(
actionButton("downloadBtn", "Download Excel")
),
mainPanel(
dataTableOutput("table1"),
dataTableOutput("table2")
)
)
)
server <- function(input, output) {
# Sample data tables
table1 <- reactive({
data.frame(
Column1 = sample(1:100, 10),
Column2 = sample(1:100, 10)
)
})
table2 <- reactive({
data.frame(
ColumnA = sample(letters, 10),
ColumnB = sample(LETTERS, 10)
)
})
output$table1 <- renderDataTable({
table1()
})
output$table2 <- renderDataTable({
table2()
})
observeEvent(input$downloadBtn, {
# Create a workbook and add worksheets
wb <- createWorkbook()
addWorksheet(wb, "Table1")
addWorksheet(wb, "Table2")
# Write data to the worksheets
writeData(wb, "Table1", table1())
writeData(wb, "Table2", table2())
# Save the workbook to a temporary file
file <- tempfile(fileext = ".xlsx")
saveWorkbook(wb, file, overwrite = TRUE)
# Send the file to the user
shiny::showModal(modalDialog(
title = "Download",
downloadButton("downloadExcel", "Download Excel"),
easyClose = TRUE,
footer = NULL
))
output$downloadExcel <- downloadHandler(
filename = function() {
paste("data_tables", Sys.Date(), ".xlsx", sep = "")
},
content = function(file) {
file.copy(file, file)
}
)
})
}
shinyApp(ui = ui, server = server)