dreamRs / shinyWidgets

shinyWidgets : Extend widgets available in shiny
https://dreamrs.github.io/shinyWidgets/
GNU General Public License v3.0
833 stars 153 forks source link

Select one element per group did not work after updatePickerInput ('max-options-group' = 1) #670

Closed jonasmuench closed 8 months ago

jonasmuench commented 8 months ago

If I update a picker input with updatePickerInput, I can select more than one element per group although I have previously restricted it with the option 'max-options-group' = 1. Is this a bug or am I missing something?

library(shiny)
library(shinyWidgets) # 0.8.1

ui <- fluidPage(

  # Select ohne Element per Group
  pickerInput(inputId = "test",
                            label = "Picker",
                            choices = list("Group1" = list("A" = "A1", "B" = "B1"),
                                           "Group2" = list("A" = "A2", "B" = "B2", "C" = "C2"),
                                           "Group3" = list("A" = "A3", "B" = "B3", "C" = "C3")),
                            selected = c("A1", "B2", "C3"),
                            multiple = TRUE,
                            options = list(`max-options-group` = 1L,
                                           title = "Select one per group",
                                           `selected-text-format` = "count > 1",
                                           `count-selected-text` = "{0} Groups")

  ),
  actionButton(inputId = "update_test",
               label = "Update the picker"
  ),
  br(),
  verbatimTextOutput(outputId = "res")
)

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

  observeEvent(input$update_test, {
    updatePickerInput(session = session,
                                    inputId = "test",
                                    label = "Updated picker",
                                    choices = list("Group1" = list("A" = "A1", "B" = "B1"),
                                                   "Group2" = list("A" = "A2", "B" = "B2", "C" = "C2"),
                                                   "Group3" = list("A" = "A3", "B" = "B3", "C" = "C3")),
                                    selected = c("A1", "B2", "C3")
    )
  })

  output$res <- renderPrint({
    input$test
  })
}

shinyApp(ui, server)

After update: image

pvictor commented 8 months ago

Thanks for reporting this, it's fixed if you re-install from GitHub, but you have to repeat the option in updatePickerInput(), e.g. :

updatePickerInput(
  session = session,
  inputId = "test",
  label = "Updated picker",
  choices = list(
    "Group1" = list("A" = "A1", "B" = "B1"),
    "Group2" = list("A" = "A2", "B" = "B2", "C" = "C2"),
    "Group3" = list("A" = "A3", "B" = "B3", "C" = "C3")
  ),
  selected = c("A1", "B2", "C3"),
  options = list(`max-options-group` = 1L)
)
jonasmuench commented 8 months ago

Thanks, when will there be a new version on CRAN?

pvictor commented 8 months ago

On CRAN now ;)