I am attempting to create two drop downs in shiny - one a list of states that then filters the second drop down which lists all counties for the selected state. The following code works BUT the counties do not update when you change states.
UI
pageWithSidebar(
headerPanel("My chart"),
sidebarPanel(
uiOutput("sel_state"),
uiOutput("sel_county")
),
mainPanel(
tableOutput("table")
)
)
Server
function(input, output, session) {
output$sel_state <- renderUI({
selectizeInput('state', 'Select a State', choices=c("Choose One" = "", state_list))
})
output$sel_county <- renderUI({
county_list <- reactive({
df %>%
filter(state == input$state) %>%
pull(county) %>%
sort() %>%
as.character()
})
selectizeInput('county', 'Select a County', choices=c("Choose One" = "", county_list()))
})
tab <- reactive({
df %>%
filter(state == input$state) %>%
filter(county == input$county)
})
output$table <- renderTable({
tab()
})
}
I attempted to replace the 'selectizeInput' in the county drop down with the following 'updateSelectizeInput' but keep getting the following error
New Server line
updateSelectizeInput(session, 'county', 'Select a County', choices=c("Choose One" = "", county_list()))
ERROR: cannot coerce type 'environment' to vector of type 'character'
Code to create data frames - the county list is not a proper list yet
x <- getURL("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv")
csv <- read.csv(text=x)
df <- as.data.frame(csv)
state_list <- c(levels(df$state))
county_list <- subset(df, select = c(3,2))
dput output
dput(head(cl))
structure(list(state = c("Washington", "Washington", "Washington",
"Illinois", "Washington", "California"), county = c("Snohomish",
"Snohomish", "Snohomish", "Cook", "Snohomish", "Orange")), row.names =
c(NA, 6L), class = "data.frame")