Skip to content

updateSelectizeInput does not properly clear existing choices #4164

@santikka

Description

@santikka

System details

Output of sessionInfo():

R version 4.4.1 (2024-06-14 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 10 x64 (build 19045)

Matrix products: default


locale:
[1] LC_COLLATE=Finnish_Finland.utf8  LC_CTYPE=Finnish_Finland.utf8    LC_MONETARY=Finnish_Finland.utf8 LC_NUMERIC=C                     LC_TIME=Finnish_Finland.utf8    

time zone: Europe/Helsinki
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] shiny_1.9.1

loaded via a namespace (and not attached):
 [1] digest_0.6.37     later_1.4.1       R6_2.5.1          httpuv_1.6.15     fastmap_1.2.0     magrittr_2.0.3    htmltools_0.5.8.1 lifecycle_1.0.4   promises_1.3.2    cli_3.6.3        
[11] xtable_1.8-4      compiler_4.4.1    rstudioapi_0.17.1 tools_4.4.1       mime_0.12         Rcpp_1.0.13-1     rlang_1.1.4 

Example application or steps to reproduce the problem

library("shiny")

ui <- fluidPage(
  uiOutput("selectize"),
  verbatimTextOutput('row'),
  title = 'Using options groups for server-side selectize input'
)

server <- function(input, output, session) {

  rv <- reactiveValues(init = FALSE)

  observe({
    rv$init <- FALSE
    Titanic2 <- as.data.frame(Titanic, stringsAsFactors = FALSE)
    Titanic2 <- cbind(Titanic2, value = seq_len(nrow(Titanic2)))
    Titanic2$label <- apply(Titanic2[, 2:4], 1, paste, collapse = ', ')
    updateSelectizeInput(
      session,
      'group',
      choices = NULL
    )
    updateSelectizeInput(
      session,
      'group',
      choices = Titanic2,
      options = list(
        placeholder = 'Select a category',

          # predefine all option groups
        optgroups = list(
          list(value = '1st', label = 'First Class'),
          list(value = '2nd', label = 'Second Class'),
          list(value = '3rd', label = 'Third Class'),
          list(value = 'Crew', label = 'Crew')
        ),

        # 'Class' is a field in Titanic2 created in server.R
        optgroupField = 'Class',

        optgroupOrder = c('1st', '2nd', '3rd', 'Crew'),

        # you can type and search in these fields in Titanic2
        searchField = c('Sex', 'Age', 'Survived'),

        # how to render the options (each item is a row in Titanic2)
        render = I("{
          option: function(item, escape) {
            return '<div>' + escape(item.Age) + ' (' +
                    (item.Sex == 'Male' ? '&male;' : '&female;') + ', ' +
                    (item.Survived == 'Yes' ? '&hearts;' : '&odash;') + ')' +
                   '</div>';
            }
          }"
        )
      ),
      server = TRUE
    )
  })

  output$row <- renderPrint({
    validate(need(
      input$group, 'Please type and search (e.g. Female)'
    ))
  })

  output$selectize <- renderUI({
    rv$init <- TRUE
    selectizeInput('group', label = "Label", choices = NA)
  })

}

shinyApp(ui = ui, server = server)

Describe the problem in detail

I'm trying to create a dynamically generated selectizeInput with optgroups (based on this example https://github.com/rstudio/shiny-examples/tree/main/023-optgroup-server). If the initial choices is not NULL, updateSelectizeInput creates a strange additional undefined option at the top of the list:
Image
If instead choices is initially NULL, this option does not appear:
Image
I would expect the initial choices to be cleared by the update despite the initial values, but this does not seem to be the case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions