Combining Download Buttons into a Single Button with Options Using Shiny in R

Combining Download Buttons into a Single Button with Options

In this article, we will explore how to combine multiple download buttons into a single button that displays options when clicked. We will use the Shiny framework for R to achieve this.

Introduction

As developers, we often find ourselves dealing with multiple download buttons in our applications. While these buttons serve their purpose, they can also clutter the user interface and make it less visually appealing. In this article, we will discuss how to combine multiple download buttons into a single button that displays options when clicked using Shiny.

Understanding Download Buttons

Before we dive into combining download buttons, let’s take a quick look at how they work in Shiny. A download button is created using the downloadButton function from the Shiny library. This function takes two arguments: the name of the download button and the label or text to be displayed.

output$downloadButton <- downloadHandler(
  filename = function() {
    # Create a file name based on the current time
    paste("Downloaded Data", Sys.time(), ".csv")
  },
  content = function(file) {
    # Write data to the file
    write.csv(data, file, row.names = FALSE)
  }
)

In this example, we create a download button with the name downloadButton and label “Downloaded Data”. When clicked, the button writes the provided data to a CSV file.

Combining Download Buttons

Now that we understand how to create individual download buttons, let’s combine multiple buttons into a single button. To achieve this, we will use the Modal function from Shiny. The Modal function allows us to create a popup window that appears when a button is clicked.

Here is an example of how to combine multiple download buttons into a single button with options:

library(shiny)
library(shinydashboard)

header <- dashboardHeader(title = "MRO Dash")
sidebar <- dashboardSidebar(actionButton("downloadBT", "Downloads", icon = icon("download")))
body <- dashboardBody(
  tags$head(tags$style("#test .modal-body {width: auto; height: auto;}"))
)

ui <- dashboardPage(header, sidebar, body)

server <- function(input, output, session) {
  myModal <- function() {
    div(id = "test",
      modalDialog(downloadButton("download1","Download Shipments tonight let's go"),
                  br(),
                  downloadButton("download2","Download Shipments"),
                  easyClose = TRUE, title = "Download Table")
    )
  }

  # open modal on button click
  observeEvent(input$downloadBT,
               ignoreNULL = TRUE,   # Show modal on start up
               showModal(myModal())
  )

  output$download1 <- downloadHandler(
    filename = function() {
      # Create a file name based on the current time
      paste("MTD of SBU Shipments ", Sys.time(), ".csv")
    },
    content = function(file) {
      # Write data to the file
      write.csv(data, file, row.names = FALSE)
    }
  )

  output$download2 <- downloadHandler(
    filename = function() {
      # Create a file name based on the current time
      paste("MTD of SBU Shipments ", Sys.time(), ".csv")
    },
    content = function(file) {
      # Write data to the file
      write.csv(data, file, row.names = FALSE)
    }
  )

}

shinyApp(ui, server)

In this example, we create a download button with the name downloadBT and label “Downloads”. When clicked, the button opens a modal window that contains two additional download buttons. Each button writes data to a separate CSV file.

Conclusion

Combining multiple download buttons into a single button is a useful technique for improving the user interface of your application. By using the Modal function from Shiny, you can create a popup window that appears when a button is clicked and displays options for downloading different files. This approach makes it easier for users to access frequently used features without cluttering the main interface with unnecessary buttons.

Additional Tips and Variations

  • You can customize the appearance of the modal window by using CSS styles. In this example, we use tags$style to set the width and height of the modal body.
  • You can add more download buttons or options to the modal window by creating additional functions that handle the button clicks. For example, you could create a function that writes data to a CSV file with specific formatting or encoding.
  • To improve accessibility, you should ensure that your application’s modal windows are accessible to users with disabilities. This can be achieved by using ARIA attributes and ensuring that the modal window has sufficient color contrast.

By following these tips and variations, you can create a more user-friendly and efficient download button system for your Shiny application.


Last modified on 2023-11-17