Download .csv within Shiny Dashboard results in UI .html download

I experienced the following behavior of Shiny dashboard: If I click on the download button to download a csv using a dashboard UI I always get the HTML of the UI downloaded. The same server-code using the standard fluidPage UI works nicely.
Here the code with dashboard:

library(shiny)
library(shinydashboard)

ui <- dashboardPage(skin=c("red"),
                    dashboardHeader(title=h4("Machine Learning"), titleWidth = 350),
                    dashboardSidebar(
                        tabsetPanel(id= "tsp",
                                  type = "tabs",
                                  downloadButton("downloadData", "Download")
                      )
                    ),#
                    dashboardBody( )
)

server <- function(input, output) {
  output$downloadData <- downloadHandler(
    
    filename = function() {
      paste("TrueCorrelation", Sys.Date(),".csv", sep = "")
    },
    content = function(file) { write.csv(mtcars, file, row.names = FALSE) } 
  )
  
}
# Run the application 
shinyApp(ui = ui, server = server)

and here with fluidPage:

library(shiny)

ui <- fluidPage(
   titlePanel("Downloading Data"),
  sidebarLayout(
       sidebarPanel(downloadButton("downloadData", "Download")
),
mainPanel(
  tableOutput("table")
)))

server <- function(input, output) {
  
  output$downloadData <- downloadHandler(
    
    filename = function() {
      paste("TrueCorrelation", Sys.Date(),".csv", sep = "")
    },
    content = function(file) { write.csv(mtcars, file, row.names = FALSE) } 
  ) 
}
shinyApp(ui = ui, server = server)

Is there a possibility to overcome this, as I prefer the dashboard UI? Thank you very much for your help

Your problem is caused by usig downloadButton, or really anything thats not a tabPanel inside of tabsetPanel

dashboardSidebar(
 tabsetPanel(id= "tsp",
type = "tabs",
 downloadButton("downloadData",
 "Download")) )

Therefore you could either wrap it in a tabPanel

   dashboardSidebar(
tabsetPanel(id= "tsp",
type = "tabs",
tabPanel("tabTitle", 
downloadButton("downloadData", 
"Download"))))

or skip tabsetPanel all together

dashboardSidebar( 
downloadButton("downloadData",
 "Download"))
1 Like

Dear nirgrahamuk, thank you very much; problem resolved.

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