Okay, so I've tried to make a zip download, and it isn't working--whether or not the "include parameters" checkbox is clicked, and no matter what I specify for a filename, I get a download called "qwe_download.html" that seems to be an html version of the shiny app itself, not a csv or zip file of the data.
Here is a minimal (I think?) toy example. I think my problem is that I really don't understand what's going on with all the functions inside downloadHandler: I've adapted this code from examples I found online where people were using for-loops, but I don't think it's working for my case. (Note that here I've written to a .txt file instead of a .json, but I may change that later).
Any thoughts on where I might be tripping up?
Thank you!
library(DT)
library(zip)
ui <- fluidPage(
fluidRow(
column(4,
downloadButton("download", "Download current data as csv", class = "btn-success"),
checkboxInput("paramsInclude", "Include parameters?", value = FALSE)
),
br(),
DT::DTOutput("table1") # table output at the bottom
)
)
server <- function(input, output, session) {
tab <- data.frame(number = c(1:10), letter = letters[1:10])
params <- data.frame(parameters = c("1 through 10", "a through j"))
output$table1 <- DT::renderDT({tab})
observeEvent(input$download, {
if(input$paramsInclude){
output$downloadData <- downloadHandler(
filename = function() {
paste("dataDownload.", gsub("-", "", Sys.Date()), ".zip")
},
content = function(fname) {
fs <- c()
tmpdir <- tempdir()
setwd(tempdir())
write.table(params, file = "params.txt", row.names = F)
write.csv(tab, file = "data.csv", row.names = F)
fs <- c("params.txt", "data.csv")
zip(zipfile = fname, files = fs)
},
contentType = "application/zip"
)
}
})
}
shinyApp(ui, server)