Hello,
I'm trying to use selectizeInput for auto-filling a form with a dataframe.
Basically I'd like that depending in which field the user is typing, the other fields are updated.
If the user selects a city, the corresponding postcodes will appear in the postcode field choices. Once a postcode is selected, the locality field is updated. This is the easy part...
But how can I implement this behavior also for the other fields :
select postcode -> update city and update locality choices
select locality -> update postcode -> update city
knowing that :
1 city = 1 or many postcodes
1 postcode = 1 city
1 postcode = 1 or many localities
1 locality = 1 postcode
Thank you for your help.
data <- data.frame(city_name = c("CITY01","CITY06","CITY07","CITY08","CITY08","CITY08","CITY08","CITY09","CITY09","CITY10","CITY10","CITY10","CITY10","CITY10","CITY10","CITY10"),
postcode = c(47925,47931,47920,47919,47919,47922,47919,47940,47940,47900,47900,47900,47917,47917,47990,47990),
locality = c("LOC00",NA,"LOC04","LOC06","LOC07","LOC08",NA,"LOC09",NA,"LOC11","LOC15","LOC20","LOC14","LOC17","LOC12","LOC13"))
ui <- shinyUI(fluidPage(
titlePanel("Auto Fill"),
sidebarPanel(
selectizeInput("city_name", choices = data[order(data$city_name), 'city_name'], selected = NULL, label = NULL, options = list(
plugins = list('restore_on_backspace'),
onFocus = I("
function() {
this.clear();
}")
)
),
selectizeInput("postcode", choices = data$postcode, selected = NULL, label = NULL, options = list(
plugins = list('restore_on_backspace')
)
),
selectizeInput("locality", choices = data$locality, selected = NULL, label = NULL, options = list(
plugins = list('restore_on_backspace')
)
)
)
)
)
server <- shinyServer(function(input, output, session) {
observe({
cdp <- data[data$city_name == input$city_name, ]$postcode
updateSelectInput(session, "postcode", choices = cdp[order(cdp)], selected = NULL)
})
observe({
loc <- data[data$postcode == input$postcode, ]$loc
updateSelectInput(session, "locality", choices = loc[order(loc)])
})
})
shinyApp(ui = ui, server = server)