How to create a simple drop down menu in shiny?

16.2k views Asked by At

So I have three download buttons in three tab panels inside a tabsetpanel in my shiny app. Is there any way I cant create a drop down menu instead of these tab panels? I don't want to mess with the outputs, just organize the code below in the form of a dropdown menu instead of tab panels.

tabsetPanel(tabPanel("download 1", downloadBttn("downloadData",size="sm","Download1"),tabPanel("download 2",downloadBttn("downloadData",size="sm","Download2"),tabPanel("download 3",downloadBttn("downloadData",size="sm","Download3"))
1

There are 1 answers

0
Mr.Rlover On

Use select input so that user simply chooses what data to download from a drop down box. No need to have separate tabs for data download

library(shiny)

ui <- fluidPage(
  selectInput("download", "Select Data to download", choices = c("euro", "mtcars", "iris")),
  downloadButton("downloadData")
)

server <- function(input, output, session) {

  dataDownload <- reactive({
    switch(input$download,
           "euro" = euro,
           "mtcars" = mtcars,
           "iris" = iris)
  })

  output$downloadData <- downloadHandler(
    filename = function() {
      paste(input$download, ".csv", sep = "")
    },
    content = function(file) {
      write.csv(dataDownload(), file, row.names = FALSE)
    }
  )

}

shinyApp(ui, server)