Reputation: 115
I am constructing my first shiny app. As part of this project I want to make downloads possible to visitors. Specifically, I want visitors to be able to download the data they selected as a csv file but inside a zip folder. So that when visitors click on the download button they get on their computer a zip folder with inside the csv of the data they selected. I want to do it this way because there are other files I will add to the download so that zip folder is the best way to do this.
I wrote the code below, unfortunately the code does not yield the expected output in the sense that nothing gets downloaded and I get the following message:
Any help?
Listening on http://127.0.0.1:5942
zip warning: missing end signature--probably not a zip file (did you
zip warning: remember to use binary mode when you transferred it?)
zip warning: (if you are trying to read a damaged archive try -F)
zip error: Zip file structure invalid
# Creation of APP ------------------------------------------
remove(ui, server)
if (interactive()) {
library(shiny)
library(shinyWidgets)
library(shinythemes)
library(shinycssloaders)
library(shinydashboard)
##########
choices_picker_cny <- unique(codebook$Polygon)
names(choices_picker_cny) <- unique(codebook$Label)
# Define UI -----------------------------------------------
ui <- fluidPage(
# Application title
titlePanel("Example Shiny App"),
# Parameters
sidebarLayout(
sidebarPanel(
selectInput(inputId = "input_period", label = "Period",
choices = c("2001" = "year_1", "2002" = "year_2", "2003" = "year_3")),
pickerInput(
inputId = "picker_cny",
label = "Select Polygon",
choices = choices_picker_cny,
options = list(`actions-box` = TRUE),
multiple = TRUE),
selectInput(inputId = "input_variable", label = "Variable",
choices = c("Variable A" = "Variable1", "Variable B" = "Variable2", "Variable C" = "Variable3", "Variable D" = "Variable4")),
downloadButton('downloadData', 'Download Selected Data', icon = shiny::icon("download")),
width = 2),
# Displat the reactive map
mainPanel(
DTOutput("t1"),
width = 10)
))
# Define Server ------------------------------------------
server <- function(input, output, session) {
output$t1 <- renderDT({
# Display data only when one polygon is selected
code1 <- codebook[codebook$Year == input$input_period & codebook$Variable == input$input_variable & (codebook$Polygon %in% input$picker_cny),]
code1
})
# Reactive value for selected dataset ----
datasetInput <- reactive({
codebook[codebook$Year == input$input_period & codebook$Variable == input$input_variable & (codebook$Polygon %in% input$picker_cny),]
})
output$downloadData <- downloadHandler(
filename = function() {
paste("extract_", print(Sys.Date()), ".zip", sep = "")
},
content = function(file) {
write.csv(datasetInput(), file, row.names = FALSE)
zip(file, "extract.csv")
},
contentType = "application/zip"
)
# Reactive pickerInput ---------------------------------
observeEvent(input$input_period, {
# Generate reactive picker input
code1 <- codebook[codebook$Year %in% input$input_period,]
codeu <- unique(codebook$Polygon)
code1u <- unique(code1$Polygon)
disabled_choices <- ifelse(codeu %in% code1u, 0,1)
#print(disabled_choices)
updatePickerInput(session = session,
inputId = "picker_cny",
choices = choices_picker_cny,
choicesOpt = list(
disabled = disabled_choices,
style = ifelse(disabled_choices,
yes = "color: rgba(119, 119, 119, 0.5);",
no = "")
)
)
}, ignoreInit = TRUE)
}
# Run the application
shinyApp(ui = ui, server = server)
}
Here is the data:
structure(list(X = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), Polygon = c("polygon_a",
"polygon_a", "polygon_a", "polygon_a", "polygon_a", "polygon_a",
"polygon_a", "polygon_a", "polygon_b", "polygon_b", "polygon_b",
"polygon_b", "polygon_b", "polygon_b", "polygon_b", "polygon_b",
"polygon_c", "polygon_c", "polygon_c", "polygon_c"), Label = c("Baron Dubois",
"Baron Dubois", "Baron Dubois", "Baron Dubois", "Baron Dubois",
"Baron Dubois", "Baron Dubois", "Baron Dubois", "Baron Delasalle",
"Baron Delasalle", "Baron Delasalle", "Baron Delasalle", "Baron Delasalle",
"Baron Delasalle", "Baron Delasalle", "Baron Delasalle", "Baron Istog",
"Baron Istog", "Baron Istog", "Baron Istog"), Year = c("year_1",
"year_1", "year_1", "year_1", "year_2", "year_2", "year_2", "year_2",
"year_1", "year_1", "year_1", "year_1", "year_2", "year_2", "year_2",
"year_2", "year_1", "year_1", "year_1", "year_1"), Variable = c("Variable1",
"Variable2", "Variable3", "Variable4", "Variable1", "Variable2",
"Variable3", "Variable4", "Variable1", "Variable2", "Variable3",
"Variable4", "Variable1", "Variable2", "Variable3", "Variable4",
"Variable1", "Variable2", "Variable3", "Variable4"), Value = c(1L,
245L, 23L, 2L, 0L, 34L, 1L, 245L, 1L, 23L, 2L, 0L, 0L, 34L, 0L,
34L, 0L, 34L, 90L, 9L)), class = "data.frame", row.names = c(NA,
-20L))
Thank you!
Upvotes: 0
Views: 156
Reputation: 84529
I would try:
content = function(file) {
tmpCSV <- tempfile(fileext = ".csv")
write.csv(datasetInput(), tmpCSV, row.names = FALSE)
zip(file, tmpCSV)
},
Upvotes: 1