您可以使用带有disable 功能的shinyjs 包。您可以使用observeEvent 触发禁用,其中事件是复选框值。使用 shinyjs 的唯一注意事项是在应用的 ui 中包含函数 useShinyjs。
library(shiny)
ui <- shinyUI(fluidPage(
shinyjs::useShinyjs(),
checkboxGroupInput("a","A",choices = 1:7)
))
server <- shinyServer(function(input, output, session) {
observeEvent(input$a, shinyjs::disable("a"))
})
shinyApp(ui,server)
该包具有附加功能,例如enable 启用禁用元素或toggleState 在启用/禁用状态之间切换元素。
从 checkboxGroupInput 禁用单个复选框
shinyjs 的 disable 函数有一个选择器参数,可用于标识要禁用哪些元素,而无需使用其闪亮的 id。我确定传递给选择器的正确查询的方法是使用SelectorGadget。小工具给出的 a 不包含shinyid 所以我添加了一个“B”复选框组,果然 id 更改为包含闪亮的 id “a”所以我相当有信心在向闪亮的应用程序添加更多元素后这应该可以工作.一旦知道选择器查询,解决方案的其余部分就是识别组中哪个复选框被单击的标签,并为disable 函数构建要使用的字符串。希望这会有所帮助。
library(shiny)
ui <- shinyUI(fluidPage(
shinyjs::useShinyjs(),
#original checkbox group
checkboxGroupInput("a","A",choices = 1:7),
#additional checkbox group to identify how to work around multiple groups
checkboxGroupInput("b","B",choices = 1:7)
))
server <- shinyServer(function(input, output, session) {
#initialize reactive values (will use to store selected boxes to identify newest selection)
rv <- reactiveValues()
#initialize selected boxes to NULL
rv$selectedBoxes <- NULL
observeEvent(input$a, {
#extract newest selection by identifying which values are in a that have not been previously selected
newSelection <- input$a[which(!(input$a %in% rv$selectedBoxes))]
#create object that identifies newly selected checkbox (syntax found using selectorgadget)
subElement <- paste0("#a .checkbox:nth-child(", newSelection,") label")
#disable single checkbox of group
shinyjs::disable(selector=subElement)
#store all selected checkboxes
rv$selectedBoxes <- input$a
})
})
shinyApp(ui,server)