Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating choices using updateSelect(ize)Input causes selection to be cleared #4118

Open
jcheng5 opened this issue Aug 23, 2024 · 4 comments
Open

Comments

@jcheng5
Copy link
Member

jcheng5 commented Aug 23, 2024

With selectizeInput(multiple = TRUE), calling updateSelectizeInput(session, "id", choices = ...) causes the selection to reset, even if the currently selected item is in the set of choices. (Doesn't happen without multiple = TRUE.)

Same with selectInput/updateSelectInput, with both selectize=TRUE and FALSE.

System details

shinylive

Example application or steps to reproduce the problem

https://shinylive.io/r/editor/#code=NobwRAdghgtgpmAXGKAHVA6ASmANGAYwHsIAXOMpMAGwEsAjAJykYE8AKAZwAtaJWAlAB0IdJiw71OY4RBEBiAAQAZWp1KKiAM0UATKK06adpbnEUB3OHADWI-YYD62x1duKAPAFpFBdkLAAWRIHANxFAIAVAFc4TlC8CLAAdThdCDiE8KjuaMZ4gzCkgDFGWiykgGUoUjyKgMroiATZEWjaTx9UKABzOEctanbdfwhFXxYRkXHxzjhqOAJSWgAvOABJCFRo0n8wB0c5haWihvnFjSg9QsSCbiJaAjjFAF5rpxc3G3Cji7TXpLBZo3cIwaLUZaoBYAyJYACqAFEBLhpjMoEtaCQAEI7UgkPbRVD6ciOeikOSJAJwok1cwAYXujziAWRqPG5AAHqQAPI7ba7AK-JZpRwtVGyVoQOaMABucEYnUUWiaGPxfH54SIfJ2PzinExEAEihAqK1pH5ABIheRdKKDIrGBRdPLInAuewTWMZt11HA9gBNLWKa1pRBFdU7C0Ha2ycYAX1jES9RCk8rlCLlZHYEdIFsJxP6ZIg4U9M0U+dplXOGLWm35XD1Buy+wMh2rpCKdweTyMbwcnGcWlc1m+wfb-zefgEiYTIjjIhEPD4rAAguh2O1dbL5bIwHGALpAA

Click the button, see that the selection is reset.

@gadenbuie
Copy link
Member

Here's a modified version of the example app that shows the behavior of selectizeInput() with both multiple = TRUE and multiple = FALSE in the same app.

Example app
library(shiny)
library(bslib)

# List of days of the week
days_of_week <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")

ui <- page_fluid(
  card(
    layout_columns(
      selectizeInput("day_select_mult", "Select a day", choices = days_of_week[1:5], selected = "Monday", multiple = TRUE),
      selectInput("day_select_single", "Select a day", choices = days_of_week[1:5], selected = "Monday", multiple = FALSE)
    ),
    input_switch("include_weekends", "Include Weekends"),
    textOutput("selected_day")
  )
)

server <- function(input, output, session) {
  output$selected_day <- renderText({
    paste("You selected:", paste(input$day_select, collapse = ", "))
  })
  
  observeEvent(input$include_weekends, {
    new_choices <- if (input$include_weekends) days_of_week else days_of_week[1:5]
    
    updateSelectizeInput(
      session,
      "day_select_mult",
      choices = new_choices
      # selected = intersect(input$day_select, new_choices)
    )

    updateSelectizeInput(
      session,
      "day_select_single",
      choices = new_choices
    )
  }, ignoreInit = TRUE)
}

shinyApp(ui, server)

@gadenbuie
Copy link
Member

This bug has been around for a bit, I can reproduce with at least Shiny 1.7.0 (Sept 2021). (Note it might go further back, I just picked a sufficiently old version to make sure this isn't related to work I did earlier this year.)

@nirguk
Copy link

nirguk commented Nov 11, 2024

In my experience the following idiom on the R side is quite common

  observeEvent(input$update_btn, {
    sel <- c()
    prev_sel <- intersect(input$day_select,days_of_week)
    if(isTruthy(prev_sel)){sel <- prev_sel}
    updateSelectizeInput(session, 
                               "day_select", 
                               choices = days_of_week, 
                               selected = sel)
  })
}

perhaps it points the way to a solution ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants