Trying to update the selected values of two pickerInput depending on the selection of the third one leads to malfunctionning of the widgets themselves (impossible to select/deselect certain values, other pickers not updating).
An example using the starwars dataset below with the expected outcomes.
if (interactive()) {
library(shiny)
library(shinyWidgets)
library(dplyr)
df <- head(starwars, 20)
ui <- fluidPage(
pickerInput(inputId = "p1",
label = "Hair color",
choices =df$hair_color %>% unique(),
selected = df$hair_color %>% unique(),
multiple = T),
pickerInput(inputId = "p2",
label = "Skin color",
choices = df$skin_color %>% unique(),
selected = df$skin_color %>% unique(),
multiple = T),
pickerInput(inputId = "p3",
label = "Eye color",
choices = df$eye_color %>% unique(),
selected = df$eye_color %>% unique(),
multiple = T),
hr(),
p("Example 1:"),
p("When selecting 'Hair color' = 'blond':"),
p("• Selected values in 'Skin color' picker should be 'fair'"),
p("• Selected values in 'Eye color' picker should be 'blue'"),
br(),
p("Example 2:"),
p("When selecting 'Skin color' = 'light':"),
p("• Selected values in 'Hair color' picker should be 'brown', 'brown, grey', 'black' and 'none'"),
p("• Selected values in 'Eye color' picker should be 'blue', 'brown' and 'hazel"),
br()
)
server <- function(input, output, session){
observeEvent(input$p1, {
updatePickerInput(session = session,
inputId = "p2",
selected = df %>%
filter(hair_color %in% input$p1) %>%
pull(skin_color) %>%
unique())
updatePickerInput(session = session,
inputId = "p3",
selected = df %>%
filter(hair_color %in% input$p1) %>%
pull(eye_color) %>%
unique())
})
observeEvent(input$p2, {
updatePickerInput(session = session,
inputId = "p1",
selected = df %>%
filter(skin_color %in% input$p2) %>%
pull(hair_color) %>%
unique())
updatePickerInput(session = session,
inputId = "p3",
selected = df %>%
filter(skin_color %in% input$p2) %>%
pull(eye_color) %>%
unique())
})
observeEvent(input$p3, {
updatePickerInput(session = session,
inputId = "p1",
selected = df %>%
filter(eye_color %in% input$p3) %>%
pull(hair_color) %>%
unique())
updatePickerInput(session = session,
inputId = "p2",
selected = df %>%
filter(eye_color %in% input$p3) %>%
pull(skin_color) %>%
unique())
})
}
shinyApp(ui, server)
}