首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Shinyjs无法正确启用/禁用UI元素

Shinyjs无法正确启用/禁用UI元素
EN

Stack Overflow用户
提问于 2019-07-04 16:45:04
回答 1查看 251关注 0票数 1

我有以下闪亮的应用程序

代码语言:javascript
复制
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的情况下重新选择,则它仍然保持启用状态。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-04 17:20:58

我做了两个更改,它几乎如您所希望的那样工作:-将单选按钮初始化为禁用-只将一个true/false传递给if

代码语言:javascript
复制
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()解决了这个问题。

代码语言:javascript
复制
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")
        }
      }
    )
  }
))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56883944

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档