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)
Relevant Functions
-
Download Button or Link
downloadLink(outputId, label = "Download", class = NULL, ...)
-
Download Handler
downloadHandler(filename, content, contentType = NULL, outputArgs = list())
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:
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.Specify the
outputId
andlabel
parameters ofdownloadLink()
to define the identifier and label of the date selector.
In the server
function of the app:
- assign the
downloadHandler()
function to theoutput$<downloadButton_id>
. - In the
downloadHandler()
give it the filename and extension you want to want when the button is clicked. - Also provide the
content
argument a function that takesfile
as its argument. Thefile
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.
See also: Download Button