设置 pickerInput() 的最大选择数

Set the maximum number of choices made by pickerInput()

我试图在 shiny 应用程序中将 pickerInput() 做出的最大选择数限制为两个,但我无法让它工作。

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)

试试这个

columns <- as.list(names(mtcars))
type <- as.list(1:ncol(mtcars))
header <- dashboardHeader()

sidebar <- dashboardSidebar(

  fluidRow(column(12,
                  pickerInput(
                    inputId = "iss",
                    label = "Issue",
                    choices = list(Columns = columns,
                                   Type = type),
                    selected = list(columns[[1]],type[[1]]),
                    multiple = T,
                    inline=TRUE,
                    options =  list("max-options-group" = 1, `style` = "btn-info")
                  )
  ))

)

body <- dashboardBody(fluidPage())

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

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

问题是您正在使用 "max-options-group" 但您没有使用 choices 中的任何组。您必须在 pickerInput()options 参数中使用 "max-options" = 2

为了完整起见,这是您的代码的修改版本。我们不能选择超过 2 个选项:

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)