当 multiple 为真时,如何限制用户可以 select 使用 selectInput in R shiny 的选项数量?
How to limit the number of options a user can select with selectInput in R shiny when multiple is true?
我有一个 Shiny 应用程序,我希望用户能够 select 多个选项——但仅限于一定的限制。我找不到限制人数的方法。
下面是一个简单的、可重现的例子。在其中,我是用户只能 select 第一个问题最多 2/4 个选项,第二个最多 3/4 个选项,第三个问题他们想要多少个选项(否问题 3 需要修改)。
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel( selectInput("q1", label = "Choose up to 2.", choices = c(" ", "option 1", "option 2", "option 3", "option 4"), multiple = TRUE),
selectInput("q2", label = "Choose up to 3.", choices = c(" ", "option 1", "option 2", "option 3", "option 4"), multiple = TRUE),
selectInput("q3", label = "Choose as many as you want.", choices = c(" ", "option 1", "option 2", "option 3", "option 4"), multiple = TRUE)),
mainPanel(
)
)
)
server <- function(input, output) {
}
# Run the application
shinyApp(ui = ui, server = server)
您可以使用 selectizeInput 代替 selectInput 并使用 options
参数使用 maxItems
.
设置允许的最大项目数
library(shiny)
ui <- fluidPage(sidebarLayout(
sidebarPanel(
selectizeInput(
"q1",
label = "Choose up to 2.",
choices = c(" ", "option 1", "option 2", "option 3", "option 4"),
multiple = TRUE,
options = list(maxItems = 2)
),
selectizeInput(
"q2",
label = "Choose up to 3.",
choices = c(" ", "option 1", "option 2", "option 3", "option 4"),
multiple = TRUE,
options = list(maxItems = 3)
),
selectInput(
"q3",
label = "Choose as many as you want.",
choices = c(" ", "option 1", "option 2", "option 3", "option 4"),
multiple = TRUE
)
),
mainPanel()
))
server <- function(input, output) {
}
# Run the application
shinyApp(ui = ui, server = server)
我有一个 Shiny 应用程序,我希望用户能够 select 多个选项——但仅限于一定的限制。我找不到限制人数的方法。
下面是一个简单的、可重现的例子。在其中,我是用户只能 select 第一个问题最多 2/4 个选项,第二个最多 3/4 个选项,第三个问题他们想要多少个选项(否问题 3 需要修改)。
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel( selectInput("q1", label = "Choose up to 2.", choices = c(" ", "option 1", "option 2", "option 3", "option 4"), multiple = TRUE),
selectInput("q2", label = "Choose up to 3.", choices = c(" ", "option 1", "option 2", "option 3", "option 4"), multiple = TRUE),
selectInput("q3", label = "Choose as many as you want.", choices = c(" ", "option 1", "option 2", "option 3", "option 4"), multiple = TRUE)),
mainPanel(
)
)
)
server <- function(input, output) {
}
# Run the application
shinyApp(ui = ui, server = server)
您可以使用 selectizeInput 代替 selectInput 并使用 options
参数使用 maxItems
.
library(shiny)
ui <- fluidPage(sidebarLayout(
sidebarPanel(
selectizeInput(
"q1",
label = "Choose up to 2.",
choices = c(" ", "option 1", "option 2", "option 3", "option 4"),
multiple = TRUE,
options = list(maxItems = 2)
),
selectizeInput(
"q2",
label = "Choose up to 3.",
choices = c(" ", "option 1", "option 2", "option 3", "option 4"),
multiple = TRUE,
options = list(maxItems = 3)
),
selectInput(
"q3",
label = "Choose as many as you want.",
choices = c(" ", "option 1", "option 2", "option 3", "option 4"),
multiple = TRUE
)
),
mainPanel()
))
server <- function(input, output) {
}
# Run the application
shinyApp(ui = ui, server = server)