'How to enable/disable some inputs based on another in R Shiny?

I created a code in which I have 4 numericInputs named slider 1, 2, 3, and 4. Sliders 1, 2, and 3 are connected and this part of the code is doing what I want. However, I want to disable slider 1, 2, and 3 when the value of slider 0 is equal to 0 and enable when slider0=1. Below a reproducible example:

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      numericInput("slider0", "Slider 0: ", min = 0, max = 1, value = 1, step=1),
      numericInput("slider1", "Slider 1: ", min = 0, max = 1, value = 0.25, step=0.05),
      uiOutput("slider2"),
      uiOutput("slider3")),
    ))



server <- function(input, output) {
  output$slider2 <- renderUI({
    numericInput("slider2", "Slider 2", min = 0,  max = 1 - input$slider1, value = 0, step=0.05)
  }) 
  
  output$slider3 <- renderUI({
    numericInput("slider3", "Slider 3", min= 0, value = 1-input$slider1-input$slider2, max=1)
  })
  
  observeEvent(input$slider0, {
    if(input$slider0 == 0){
      shinyjs::disable("slider1")
    } else {
      shinyjs::enable("slider1")
    }
  })
  
  observeEvent(input$slider0, {
    if(input$slider0 == 0){
      shinyjs::disable("slider2")
    } else {
      shinyjs::enable("slider2")
    }
  })
  
  observeEvent(input$slider0, {
    if(input$slider0 == 0){
      shinyjs::disable("slider3")
    } else {
      shinyjs::enable("slider3")
    }
  })
  
}

runApp(list(ui = ui, server = server))



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source