Shinyjs 不能正确地 enable/disable UI 元素

Shinyjs cant enable/disable UI element properly

我有以下 Shiny 应用程序

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")
            }
        })
    }
))

当我 select a, b, c, 启用 Radiobutton 时,现在如果我删除所有 select 离子,我仍然会看到它已启用。这对我来说很奇怪,因为我希望它会被禁用。然后在删除所有之后,如果我在没有 a 的情况下制作一个新的 selection 那么它仍然保持启用状态。

有人可以告诉我哪里出了问题吗?

我进行了两项更改,它几乎可以如您所愿地运行: - 将单选按钮初始化为禁用 - 仅将一个 true/false 传递给 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"

之后什么都没有选择时按钮一直处于启用状态

编辑:当输入值为空时,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")
        }
      }
    )
  }
))