My server.R has the following code, I would like my Shiny app to chose States based on the Country Selection, but it shows all states in the second line. I guess here, observe
is only observing not performing any action.
shinyServer(function(input, output, session) {
observe({
ddf = df[df$Date>=input$daterange[1] & df$Date<=input$daterange[2],]
updateSelectInput(session, "Country", choices = ddf$Country)
ddf1 = subset(ddf, grepl(input$Country, ddf$State))
updateSelectInput(session, "State", choices = ddf1$State)
})
}
Based on above selection, I want to pass some data frame for plotting. When I select different country it is changing states list for a second and going back to all First country's state list. I really appreciate if some one can show an example here. My ui.R code is below
sidebarPanel(
wellPanel(dateRangeInput("daterange", "Date range:",
Sys.Date()-10,
Sys.Date()+10)),
wellPanel(selectInput("Country", "Select a Country:",
'')),
wellPanel(selectInput("State", "Select a State:",
'')))
I think there's like a conflict in your observer cause it contains
input$Country
as well as an updater for theCountry
input. Then I'd try to split it into two observers, and I'd use a reactive conductor to makeddf
only once.Moreover, shouldn't you use the levels of the column rather than the column itself in the
choices
argument ?If the
Country
andState
columns are not factors but characters, useunique()
instead oflevels(droplevels())
.