问题
I want to download a csv
file using the download button in Shiny. The file would be created using the parameters from a secondary r script.
###SERVER
output$downloadData <- downloadHandler({
filename = function() {
paste('data-', Sys.Date(), '.csv', sep='')
}
content = function(file) {
csv_write<-array(0,dim=c(length(GHI_D),15))
csv_write<-cbind(GHI_Data$timeStamp,GHI_D,POA_OBS_T,POA_model_T,POA_model_FT,POA_OBS,DNI_model,DHI,tracking_angle,incidence_angel_T,Backtracking_angle,SunAz,SunEl,Kt,DNI,DDNI,incidence_angel,DHI_model,DHI_model_T,Eb,Eb_T)
write.csv(csv_write,row.names=FALSE, na="")
write.csv(csv_write,row.names=FALSE, na="")
}
})
### UI
downloadButton('downloadData', 'Download CSV Report', style="display: block; margin: 0 auto; width: 230px;color: black;")
回答1:
I think the problem with your code is that you are trying to download two CSV's from one downloadbutton. You have two variables called csv_write, and two write.csv calls. A minimal working example would look like:
library(shiny)
server <- shinyServer(function(input, output, session) {
output$downloadData <- downloadHandler(
filename = function() {
paste("dataset-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(mtcars, file)
})
})
ui <- shinyUI(fluidPage(
downloadButton('downloadData', 'Download data')
))
shinyApp(ui=ui,server=server)
来源:https://stackoverflow.com/questions/45236368/how-to-use-the-download-button-in-shiny