Search code examples
rshinyshinywidgets

Set the maximum number of choices made by pickerInput()


Im trying to limit the max number of choices made by pickerInput() to two in shiny app but I cannot make it work.

library(shiny)
library(shinydashboard)
library(plotly)
library(shinyWidgets)
header <- dashboardHeader()

sidebar <- dashboardSidebar(
  
  
  fluidRow(column(12,
                  pickerInput(
                    inputId = "iss",
                    label = "Issue", 
                    choices = colnames(mtcars),
                    multiple = T,
                    options =  list("max-options-group" = 2)
                  )           
  ))
  
)

body <- dashboardBody(fluidPage(
  
  
  )
  
)


ui <- dashboardPage(title = 'Search', header, sidebar, body)


server <- function(input, output, session) {
  
  
  
}
shinyApp(ui = ui, server = server)

Solution

  • The problem is that you are using "max-options-group" but you are not using any groups in your choices. You must use "max-options" = 2 in the options argument of pickerInput().

    For completeness, this is the modified version of your code. We cannot pick more than 2 options with it:

    library(shiny)
    library(shinydashboard)
    library(plotly)
    library(shinyWidgets)
    header <- dashboardHeader()
    
    sidebar <- dashboardSidebar(
      
      
      fluidRow(column(12,
                      pickerInput(
                        inputId = "iss",
                        label = "Issue", 
                        choices = colnames(mtcars),
                        multiple = T,
                        options =  list("max-options" = 2)
                      )           
      ))
      
    )
    
    body <- dashboardBody(fluidPage(
      
      
    )
    
    )
    
    
    ui <- dashboardPage(title = 'Search', header, sidebar, body)
    
    
    server <- function(input, output, session) {
      
      
      
    }
    shinyApp(ui = ui, server = server)