Reputation: 2018
I have a shiny app which has two selectizeinputs with identical choices from a common vector. In my dummy example, I use alpha, beta, .. echo. What I want, is to exclude a selected choice from one input from the other. So if alpha is selected in the first input, then alpha would be removed from the possible choices in the second input so it wouldn't be possible to choose alpha in both.
This is what I've attempted so far, however the choices just keeps alternating between alpha and beta.
I have also tried setting "Alpha" as the selected default input in the first input, but if I than select a different choice in the first input, than a different choice in the second input, it defaults back to "Alpha" in the first input.
library(shiny)
ui <- fluidPage(
selectizeInput("choice_1", label = "Choice 1", choices = NULL, selected = NULL),
selectizeInput("choice_2", label = "Choice 2", choices = NULL, selected = NULL),
)
server <- function(input, output, session) {
levels <- c("Alpha", "Beta", "Charlie", "Delta", "Echo")
observeEvent(input$choice_2, {
updateSelectizeInput(session,
"choice_1",
choices = levels[levels != input$choice_2])
})
observeEvent(input$choice_1, {
updateSelectizeInput(session,
"choice_2",
choices = levels[levels != input$choice_1])
})
}
# Run the application
shinyApp(ui = ui, server = server)
Upvotes: 0
Views: 43
Reputation: 125328
One Option to avoid the infinite cycle would be to set the selected=
argument. The issue is that when your app starts, both selectizeInput
s are initialized with all available choices and when selected=NULL
the first element, i.e. Alpha
, gets selected by default for both which triggers the next update round in which now Alpha
gets dropped from the choices for both and the selected element switches to Beta
for both and so on and so forth.
library(shiny)
ui <- fluidPage(
selectizeInput("choice_1", label = "Choice 1", choices = NULL, selected = NULL),
selectizeInput("choice_2", label = "Choice 2", choices = NULL, selected = NULL),
)
server <- function(input, output, session) {
levels <- c("Alpha", "Beta", "Charlie", "Delta", "Echo")
observeEvent(input$choice_2, {
updateSelectizeInput(session,
"choice_1",
choices = levels[levels != input$choice_2],
selected = input$choice_1
)
})
observeEvent(input$choice_1, {
updateSelectizeInput(session,
"choice_2",
choices = levels[levels != input$choice_1],
selected = input$choice_2
)
})
}
shinyApp(ui = ui, server = server)
Upvotes: 1