【问题标题】:Enable/disable Shiny widget based on values of 2 other Widgets根据其他 2 个小部件的值启用/禁用闪亮小部件
【发布时间】:2020-07-19 23:09:48
【问题描述】:

我想根据其他 2 个 widgets 的值启用/禁用 Shiny widget,如下所示 -

library(shiny)
library(shinyWidgets)

shinyApp(
    ui = fluidPage(
      useShinyjs(),

      prettyCheckbox(inputId = "Check1", label = "A"),
      prettyCheckbox(inputId = "Check2", label = "B"),
      uiOutput("Date_UI")
    ),
    server = function(input, output) {



      output$Date_UI = 
        renderUI(dateInput(inputId = "Date", 
                            label = NULL, 
                            width = "80%",
                            value = Sys.Date(), 
                            min = Sys.Date() - 10, 
                            max = Sys.Date() + 10) 
              )

      observeEvent(c( 
            input$Check1,
            input$Check2
            ), 
            {
              if (input$Check1 || input$Check2) {
                  enable('Date')
                } else {
                  disable('Date')
                }
            })


    }
  )

我希望这样,如果单击Check1Check2 中的任何一个,则将启用Date。因此,如果两者都未选中,则 Date 将被禁用。 GUI 应该以 Date 开头,禁用。

显然,上面的代码没有发生这种情况?你能帮忙指出哪里出了问题吗?

【问题讨论】:

    标签: r shiny shinyapps


    【解决方案1】:

    据我所知,除了应用程序的初始加载之外,您的代码似乎可以正常工作。使用disabled()(shinyjs 函数)将服务器中的 dateInput 调用包装起来,使其在加载时最初被禁用:

    output$Date_UI = 
          renderUI(disabled(dateInput(inputId = "Date", 
                             label = NULL, 
                             width = "80%",
                             value = Sys.Date(), 
                             min = Sys.Date() - 10, 
                             max = Sys.Date() + 10))
    

    【讨论】:

      猜你喜欢
      • 2021-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-01
      • 2018-12-25
      • 1970-01-01
      • 1970-01-01
      • 2022-01-26
      相关资源
      最近更新 更多