0

I am building a shiny App with a tab that should display a table based on a reactive function. The UI for that tab looks like this:

 tabPanel("Data Download", 
                 selectInput("county_5", "Choose a County", choices = unique(df$name)),
                 selectInput("response_5","Choose a Response:",
                             choices = c("Cases","Death.Due.to.Illness","Hospitalized")),
                 selectInput("dataset", "Choose a Counts or Rates:",
                             choices = c("Counts", "Rates")),
                 tableOutput("table"),
                 downloadButton("downloadData", "Download")
                 
        )

And the Server for that tab is like this

df5_c<- reactive({ df%>%
  filter(name == input$county_5) %>%
  group_by(Onset.Date) %>%
  summarize(Count=sum(as.numeric(.data[[input$response_5]])))
})

  
df5_r<-reactive({df%>%
  filter(name == input$county_5) %>%
  group_by(Onset.Date) %>%
  summarize(Rate=sum(as.numeric(.data[[input$response_5]]))/CENSUS2010POP[1]*100) 
})
  
datasetInput <- reactive({
  
  switch(input$dataset,
         "Count" = df5_c(),
         "Rate" = df5_r())
})

output$table <- renderTable({
  datasetInput()
})

output$downloadData <- downloadHandler(
  filename = function() {
    paste(input$dataset, "my_data.csv", sep = "")
  },
  content = function(file) {
    write.csv(datasetInput(), file, row.names = FALSE)
  }
)

I want the person to select either "Count" or "Rate", and based on this selection render the table for the selected input and response, and then be able to download it. Not sure what I am doing wrong but the table is not rendering and the download button shows an empty file.

1 Answers1

0

Looks like a typo. input$dataset has choices of 'Counts' and 'Rates', while the switch statement is looking for 'Count' and 'Rate'.

Including a reproducible example can make it easier and quicker for people to help you, i.e. include a sample of your data, the packages you used, etc.

Ash
  • 1,463
  • 1
  • 4
  • 7