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

updateSelectizeInput does not properly clear existing choices #4164

Closed
santikka opened this issue Dec 10, 2024 · 3 comments
Closed

updateSelectizeInput does not properly clear existing choices #4164

santikka opened this issue Dec 10, 2024 · 3 comments

Comments

@santikka
Copy link

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.

@gadenbuie
Copy link
Member

Can you please try with the latest version of Shiny installed from GitHub? There's a very good chance this issue was fixed by #4142.

# install.packages("pak")
pak::pak("rstudio/shiny")

# alternatively...
# install.packages("remotes")
remotes::install_github("rstudio/shiny")

@santikka santikka changed the title updateSelectizeInpute does not properly clear existing choices updateSelectizeInput does not properly clear existing choices Dec 10, 2024
@santikka
Copy link
Author

@gadenbuie Thank you, the issue no longer occurs with the latest development version.

@gadenbuie
Copy link
Member

Great to hear! I'm going to close this issue. We're working toward releasing Shiny this week.

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

2 participants