To create file download button, there should be a downloadButton
on the client side, and a corresponding downloadHandler
on the server side.
Created
March 18, 2014 21:49
-
-
Save wch/9630481 to your computer and use it in GitHub Desktop.
File download example for R Shiny
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Title: Download File | |
Author: RStudio, Inc. | |
AuthorUrl: http://www.rstudio.com/ | |
License: MIT | |
DisplayMode: Showcase | |
Tags: file-download | |
Type: Shiny |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
shinyServer(function(input, output) { | |
datasetInput <- reactive({ | |
# Fetch the appropriate data object, depending on the value | |
# of input$dataset. | |
switch(input$dataset, | |
"Rock" = rock, | |
"Pressure" = pressure, | |
"Cars" = cars) | |
}) | |
output$table <- renderTable({ | |
datasetInput() | |
}) | |
# downloadHandler() takes two arguments, both functions. | |
# The content function is passed a filename as an argument, and | |
# it should write out data to that filename. | |
output$downloadData <- downloadHandler( | |
# This function returns a string which tells the client | |
# browser what name to use when saving the file. | |
filename = function() { | |
paste(input$dataset, input$filetype, sep = ".") | |
}, | |
# This function should write data to a file given to it by | |
# the argument 'file'. | |
content = function(file) { | |
sep <- switch(input$filetype, "csv" = ",", "tsv" = "\t") | |
# Write to a file specified by the 'file' argument | |
write.table(datasetInput(), file, sep = sep, | |
row.names = FALSE) | |
} | |
) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
shinyUI(fluidPage( | |
titlePanel('File download'), | |
sidebarLayout( | |
sidebarPanel( | |
selectInput("dataset", "Choose a dataset:", | |
choices = c("Rock", "Pressure", "Cars")), | |
radioButtons("filetype", "File type:", | |
choices = c("csv", "tsv")), | |
downloadButton('downloadData', 'Download') | |
), | |
mainPanel( | |
tableOutput('table') | |
) | |
) | |
)) |
Same here. Any chance to fix this bug?
Same with me. Should be nice to have functionality, for trying out POC on local machine.
Works with browsers, though, so no big deal after all!
Thanks.
Is there any way I can download the multiple outputs in a single file ?
Example: Summary and structure of a data in a single file?
Is there a way to add download button to shiny flexdashboard?
@VHAnalytos you can zip the files and send the zip to the user as a response, see the accepted response in http://stackoverflow.com/questions/26881368/shiny-download-zip-archive for an example (list file paths in fs
variable).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Seems to be a problem with RStudio. I can't save files, too, while I am using the RStudio app window. If I click "Open in browser", it works fine. I'm not sure, but I think downloads within the RStudio window worked fine a few days ago...