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

Viewed 252

I want to enable/disable a Shiny widget based on the values of 2 other widgets as below -

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


    }
  )

With this I want that, if either of Check1 and Check2 is clicked then Date will be enabled. So if both were unselected then Date will be disabled. GUI should start with Date disabled.

Clearly this is not happening with above code? Can you please help to point what went wrong?

1 Answers

As far as I can tell, your code seems to work except for the initial loading of the app. Wrap the dateInput call in server with disabled() (shinyjs function) to have it initially disabled upon load:

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