【发布时间】:2025-12-18 00:50:02
【问题描述】:
我有一个 selectizeInput ,其中包含一些具有多项选择的分组元素。是否有一种优雅的方式(例如,使用 options 参数)允许每个组仅使用一个元素,以便在选择该特定组的元素时丢弃(或禁用)整个组?
到目前为止,我以编程方式尝试过,但是当更新 selectizeInput 时,selectizeInput 的下拉菜单将被关闭。
小例子:
library(shiny)
ui <- fluidPage(
selectizeInput("selInput", "Default",
choices=list(g1 = c(A="A",B="B"),
g2 = c(C="C",D="D")),
multiple=T),
selectizeInput("oneElementPerGroup", "One element per group",
choices=list(g1 = c(A="A",B="B"),
g2 = c(C="C",D="D")),
multiple=T)
)
server <- function(session, input, output) {
#Removes the corresponding groups of selected items
observeEvent(input$oneElementPerGroup, ignoreNULL = F, {
plusChoice <- input$oneElementPerGroup
names(plusChoice) <- input$oneElementPerGroup
choices <- list(g1 = c(A="A",B="B"),
g2 = c(C="C",D="D"))
if(any(input$oneElementPerGroup %in% c("A", "B"))){
choices[["g1"]] <- NULL
}
if(any(input$oneElementPerGroup %in% c("C", "D"))){
choices[["g2"]] <- NULL
}
choices$we <- plusChoice
updateSelectizeInput(session,"oneElementPerGroup",
choices = choices,
selected=input$oneElementPerGroup)
})
}
shinyApp(ui = ui, server = server)
【问题讨论】:
标签: r shiny grouping selectinput