R Shiny selectizeInput:设置输入量的最小值
R Shiny selectizeInput: set minimum for amount of inputs
我在闪亮的应用程序中有一个 selectizeInput
和 multiple = TRUE
,我想阻止用户选择 NULL
(即,将其留空)。我的目标是确保至少选择一项项(无论是哪项)。
我找到了 question on the opposite problem (i.e., limiting maximum number of selections) and I checked selectize documentation。不幸的是,似乎没有 minItems
选项。有没有办法实现我想要的功能?
最小示例:
library(shiny)
shinyApp(
ui = fluidPage(
selectizeInput(
inputId = "testSelect",
label = "Test",
choices = LETTERS[1:4],
selected = LETTERS[1],
multiple = TRUE,
# Problem: how to specify 'minItems' here
options = list(maxItems = 2)
),
verbatimTextOutput("selected")
),
server = function(input, output) {
output$selected <- renderPrint({
input$testSelect
})
}
)
似乎是一个悬而未决的问题:#https://github.com/selectize/selectize.js/issues/1228.
关于您的 R/Shiny 实施,您可以使用 renderUI()
的解决方法。
您将在服务器端构建输入并控制所选选项。
在服务器端构建输入之前,您可以检查当前值,如果它不满足您的要求,您可以覆盖它:
selected <- input$testSelect
if(is.null(selected)) selected <- choices[1]
可重现的例子:
library(shiny)
choices <- LETTERS[1:4]
shinyApp(
ui = fluidPage(
uiOutput("select"),
verbatimTextOutput("selected")
),
server = function(input, output) {
output$selected <- renderPrint({
input$testSelect
})
output$select <- renderUI({
selected <- input$testSelect
if(is.null(selected)) selected <- choices[1]
selectizeInput(
inputId = "testSelect",
label = "Test",
choices = choices,
selected = selected,
multiple = TRUE,
options = list(maxItems = 2)
)
})
}
)
我在闪亮的应用程序中有一个 selectizeInput
和 multiple = TRUE
,我想阻止用户选择 NULL
(即,将其留空)。我的目标是确保至少选择一项项(无论是哪项)。
我找到了 minItems
选项。有没有办法实现我想要的功能?
最小示例:
library(shiny)
shinyApp(
ui = fluidPage(
selectizeInput(
inputId = "testSelect",
label = "Test",
choices = LETTERS[1:4],
selected = LETTERS[1],
multiple = TRUE,
# Problem: how to specify 'minItems' here
options = list(maxItems = 2)
),
verbatimTextOutput("selected")
),
server = function(input, output) {
output$selected <- renderPrint({
input$testSelect
})
}
)
似乎是一个悬而未决的问题:#https://github.com/selectize/selectize.js/issues/1228.
关于您的 R/Shiny 实施,您可以使用 renderUI()
的解决方法。
您将在服务器端构建输入并控制所选选项。 在服务器端构建输入之前,您可以检查当前值,如果它不满足您的要求,您可以覆盖它:
selected <- input$testSelect
if(is.null(selected)) selected <- choices[1]
可重现的例子:
library(shiny)
choices <- LETTERS[1:4]
shinyApp(
ui = fluidPage(
uiOutput("select"),
verbatimTextOutput("selected")
),
server = function(input, output) {
output$selected <- renderPrint({
input$testSelect
})
output$select <- renderUI({
selected <- input$testSelect
if(is.null(selected)) selected <- choices[1]
selectizeInput(
inputId = "testSelect",
label = "Test",
choices = choices,
selected = selected,
multiple = TRUE,
options = list(maxItems = 2)
)
})
}
)