I have two selectizeInputs (state1
and state2
) in my app that have the same set of choices. When the user clicks the actionButton, I would like to compare the values of state1
and state2
and display a feedback message to the user if they are the same.
Both selectizeInputs are initialised with the placeholder "Search by name". If I change the value of one input for the first time and leave the value of the other unchanged at its placeholder value, then clicking the action button triggers the feedback message:
This is puzzling to me because the message should only be displayed if state1
and state2
are the same.
Here is the code to reproduce the above:
library(shiny)
library(shinyFeedback)
ui <- fluidPage(shinyFeedback::useShinyFeedback(),
selectizeInput('state1', 'Select state 1', choices = state.name,
multiple = T,
options = list(placeholder = "Search by name", maxItems = 1)),
selectizeInput('state2', 'Select state 2', choices = state.name,
multiple = T,
options = list(placeholder = "Search by name", maxItems = 1)),
actionButton("click", "Click"),
textOutput("states")
)
server <- function(input, output, session) {
values = reactiveValues(states = NULL)
observeEvent(input$click, {
feedbackDanger("state1", input$state1 == input$state2, "")
feedbackDanger("state2", input$state2 == input$state1, "State 1 and state 2 cannot be the same.")
req(!is.null(input$state1), !is.null(input$state2), input$state1 != input$state2)
values$states <- paste(input$state1, input$state2)
})
output$states <- renderText({
values$states
})
}
shinyApp(ui, server)
I think this may have something to do with using multiple = T and maxItems = 1 in the selectizeInputs or with the way I have written the trigger conditions but I don't understand why they are wrong. Any help would be greatly appreciated.