Download Link

#| standalone: true
#| components: [viewer]
#| viewerHeight: 380

library(shiny)
library(bslib)

# Workaround for Chromium Issue 468227
# Need this to properly download the csv file
# TODO: this is a workwround that only applies to shiny live
# we might want to make this workaround clearer that it's specific to shinylive
# and not needed in a regular shiny app
# if people navigate from the function reference, they will not see this code
# and will try to run it in shinylive with an error, but not locally
# but if they see the code in shiny live, they will end up
# putting it in the local shiny app
# suggest: putting an additional comment line that reads:

# Workaround for Chromium Issue 468227
# Need this to properly download the csv file
# this bug and workaround is only for shinylive, you do not need it in your regular app
downloadButton <- function(...) {
  tag <- shiny::downloadButton(...)
  tag$attribs$download <- NULL
  tag
}

ui <- page_fixed(
  downloadLink("downloadData", "Download mtcars")
)

server <- function(input, output) {
  output$downloadData <- downloadHandler(
    filename = "mtcars.csv",
    content = function(file) {
      write.csv(mtcars, file)
    }
  )
}

shinyApp(ui, server)
library(shiny)
library(bslib)

# Workaround for Chromium Issue 468227
# Need this to properly download the csv file
# TODO: this is a workwround that only applies to shiny live
# we might want to make this workaround clearer that it's specific to shinylive
# and not needed in a regular shiny app
# if people navigate from the function reference, they will not see this code
# and will try to run it in shinylive with an error, but not locally
# but if they see the code in shiny live, they will end up
# putting it in the local shiny app
# suggest: putting an additional comment line that reads:

# Workaround for Chromium Issue 468227
# Need this to properly download the csv file
# this bug and workaround is only for shinylive, you do not need it in your regular app
downloadLink <- function(...) {
  tag <- shiny::downloadLink(...)
  tag$attribs$download <- NULL
  tag
}

ui <- page_fixed(
  downloadLink("downloadData", "Download mtcars")
)

server <- function(input, output) {
  output$downloadData <- downloadHandler(
    filename = "mtcars.csv",
    content = function(file) {
      write.csv(mtcars, file)
    }
  )
}

shinyApp(ui, server)
No matching items

Relevant Functions

No matching items

Details

The download button or download link allows you to create an input that will initiate a browser download. It is used in conjunction with the downloadHandler() function in the server function.

To add a download button to your app:

  1. Add downloadLink to the UI of your app to create a download button. Where you call this function will determine where the download button will appear within the app’s layout.

  2. Specify the outputId and label parameters of downloadLink() to define the identifier and label of the date selector.

In the server function of the app:

  1. assign the downloadHandler() function to the output$<downloadButton_id>.
  2. In the downloadHandler() give it the filename and extension you want to want when the button is clicked.
  3. Also provide the content argument a function that takes file as its argument. The file is a temp file that will be used when the download content is written.

For Chromium-based browsers (e.g., Chromium, Google Chrom, Brave, etc.), you will also need to place the following block of code at the top of your app. Otherwise when the download button is triggered, it will download an .xml file, instead of the file you actually want.

downloadLink <- function(...) {
  tag <- shiny::downloadLink(...)
  tag$attribs$download <- NULL
  tag
}

See also: Download Button