I'm trying to create a simple selectInput dependent function for "counties" that is dependent/reactive on the user-selected "state". So, the user selects a single state in the first dropdown box, then a county dropdown box should populate with the counties for that state. A graph of # of cases is then created for the selected county. I've been swimming in circles on the best way to do this with no luck so far.
The app in its current form below is "working" but there's no dependency. So once the state is selected, all of the counties in the US are still listed. I know the issue is the selectInput statement for county -- I'm just not sure which is the best way to make it dependent on the state. I've fumbled through what seems like countless online examples, but they don't seem to fit. Any advice is much appreciated.
library(shiny)
library(dplyr)
library(ggplot2)
usdata <- read_csv("countyout.csv")
ui <- fluidPage(
titlePanel("US Counties"),
sidebarPanel(
selectInput("state", "Select State:", choices = unique(as.character(usdata$state))),
selectInput("county", "Select County:", choices = unique(as.character(usdata$county)))),
mainPanel(
plotOutput('casesplot', height = "300px")))
server <- function(input, output) {
filtered <- reactive ({
usdata %>%
filter(state == input$state) %>%
filter(county == input$county)
})
output$casesplot <- renderPlot({
ggplot(data = filtered(), aes(x=date, y=cases)) +
geom_bar(stat = "identity")
})
}
shinyApp(ui = ui, server = server)