【问题标题】:Shiny r- How to disable checkboxgroup input after it is clicked?Shiny r-单击后如何禁用复选框组输入?
【发布时间】:2017-01-09 15:20:31
【问题描述】:

有没有办法在闪亮的 r 中单击复选框组元素后禁用它?

我试图找出问题所在,但没有成功。我添加了我正在处理的复选框组。 感谢您的帮助!

library(shiny)

shinyUI(fluidPage(
  checkboxGroupInput("a","A",choices = 1:7),
  tags$head(HTML(
    '
    <script type="text/javascript">
    document.getElementById("a").onclick = function(){
        document.getElementById("a").disabled=true;
    }
    </script>
    '
  ))
  ))

shinyServer(function(input, output, session) {
  

  })

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您可以使用带有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 禁用单个复选框

    shinyjsdisable 函数有一个选择器参数,可用于标识要禁用哪些元素,而无需使用其闪亮的 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)
    

    【讨论】:

    • 谢谢!这是一个非常有用的建议。您是否认为可以仅禁用单击的元素而不是所有复选框组?
    • 由于整个复选框组是具有单个 ID 的单个元素,我不确定 shinyjs 是否可以轻松处理禁用组内单个框的问题。如果有简单的解决方案,我可以对此问题进行一些研究并报告。
    • @L.Rop 我在答案中添加了一些似乎可以完成您所追求的目标
    • 完美运行!非常感谢您的宝贵帮助!
    【解决方案2】:

    两件事:

    tags$head(HTML(
        '
        <script type="text/javascript">
        document.getElementById("a").onclick = function(){
            document.getElementById("a").disabled=true;
        }
        </script>
        '
      ))
    

    应该是:

    tags$script(HTML(
        '
        document.getElementById("a").onclick = function(){
            document.getElementById("a").disabled=true;
        }
        '
      ))
    

    这仍然不起作用,因为checkboxGroupInput 没有 onclick 事件。如果您为 "a" 使用不同的 HTML 元素,它可以工作:

    library(shiny)
    
    shinyUI(fluidPage(
      actionButton("a","A"),
      tags$head(HTML(
        '
        document.getElementById("a").onclick = function(){
            document.getElementById("a").disabled=true;
        }
        '
      ))
      ))
    
    shinyServer(function(input, output, session) {
      
    
      })

    【讨论】:

    • 代替onclick,您可能可以在文档就绪事件上运行JS代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-14
    • 2021-04-04
    相关资源
    最近更新 更多