【问题标题】:Shinyjs cant enable/disable UI element properlyShinyjs 无法正确启用/禁用 UI 元素
【发布时间】:2019-11-14 23:02:02
【问题描述】:

我有以下闪亮的应用程序

library(shiny)
runApp(shinyApp(
    ui = fluidPage(
        shinyjs::useShinyjs(),
       selectizeInput(inputId = "aaa", label = NULL, choices = c('a', 'b', 'c', 'd', 'e', 'f'), selected = NULL, width = '90%',
                         options = list(placeholder = 'Get', 
                                                onInitialize = I('function() { this.setValue(""); }'), maxItems = 5)),
       radioButtons("bbb", label = NULL, inline = TRUE, width = "100%", selected = 95,
                                                        choiceValues = list(1, 2, 3),
                                                        choiceNames = list(
                                                                            div(style = "font-size:24px;", "1%"), 
                                                                            div(style = "font-size:24px;", "2%"), 
                                                                            div(style = "font-size:24px;", "3%")
                                                                          ))
    ),
    server = function(input, output, session) {
        observeEvent(input$aaa, {
            if(grepl("a", input$aaa)){
                shinyjs::enable("bbb")
            }else{
                shinyjs::disable("bbb")
            }
        })
    }
))

当我选择a, b, c, 时,Radiobutton 启用,现在如果我删除所有选择,我仍然看到启用。这对我来说很奇怪,因为我希望它会被禁用。然后在删除所有内容后,如果我在没有a 的情况下重新选择,那么它仍然保持启用状态。

谁能告诉我哪里出了问题?

【问题讨论】:

    标签: shiny shiny-server shinyjs


    【解决方案1】:

    我做了两个更改,它几乎可以按照您的意愿工作: - 将单选按钮初始化为禁用 - 只将一个真/假传递给 if

    library(shiny)
    library(shinyjs)
    runApp(shinyApp(
      ui = fluidPage(
        shinyjs::useShinyjs(),
        selectizeInput(inputId = "aaa", label = NULL, choices = c('a', 'b', 'c', 'd', 'e', 'f'), selected = NULL, width = '90%',
                       options = list(placeholder = 'Get', 
                                      onInitialize = I('function() { this.setValue(""); }'), maxItems = 5)),
        shinyjs::disabled( # buttons disable at the launching
          radioButtons("bbb", label = NULL, inline = TRUE, width = "100%", selected = 95,
                       choiceValues = list(1, 2, 3),
                       choiceNames = list(
                         div(style = "font-size:24px;", "1%"), 
                         div(style = "font-size:24px;", "2%"), 
                         div(style = "font-size:24px;", "3%")
                       ))
        )
    
      ),
      server = function(input, output, session) {
        observeEvent(input$aaa, {
          # any(grepl(...)) returns TRUE if one of them is TRUE
          # just grepl(..) would return a vector of TRUE and FALSE
          if(any(grepl("a", input$aaa))){
            shinyjs::enable("bbb")
          }else{
            shinyjs::disable("bbb")
          }
        })
      }
    ))
    

    我不知道为什么在选择“a”后没有选择任何内容时按钮一直处于启用状态

    编辑:当输入具有值然后为 NULL 时,observeEvent 似乎没有反应。改用observe() 可以解决问题。

    library(shiny)
    library(shinyjs)
    runApp(shinyApp(
      ui = fluidPage(
        shinyjs::useShinyjs(),
        selectizeInput(inputId = "aaa", label = NULL, choices = c('a', 'b', 'c', 'd', 'e', 'f'), selected = NULL, width = '90%',
                       options = list(placeholder = 'Get', 
                                      onInitialize = I('function() { this.setValue(""); }'), maxItems = 5)),
        shinyjs::disabled(
          radioButtons("bbb", label = NULL, inline = TRUE, width = "100%", selected = 95,
                       choiceValues = list(1, 2, 3),
                       choiceNames = list(
                         div(style = "font-size:24px;", "1%"), 
                         div(style = "font-size:24px;", "2%"), 
                         div(style = "font-size:24px;", "3%")
                       ))
        )
    
      ),
      server = function(input, output, session) {
    
        observe({
            if(any(grepl("a", input$aaa))){
              shinyjs::enable("bbb")
            }else{
              shinyjs::disable("bbb")
            }
          }
        )
      }
    ))
    

    【讨论】:

      猜你喜欢
      • 2018-12-22
      • 1970-01-01
      • 2019-07-14
      • 2018-10-04
      • 2020-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多