Enable/disable 基于 2 个其他小部件的值的闪亮小部件

Enable/disable Shiny widget based on values of 2 other Widgets

我想 enable/disable 一个 Shiny widget 基于 2 个其他 widgets 的值,如下所示 -

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 开始禁用。

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

据我所知,除了应用程序的初始加载外,您的代码似乎可以正常工作。使用 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))