rshiny

Disabling manual update of r shiny sliderinput and only allowing updatesliderinput to change it


I shamelessly copied this example of co-dependent R Shiny sliderInput from another answer on this forum. I want this behavior with a minor twist. I want one of the sliders to NOT allow user input, but only update and slide when the OTHER slider is changed.

Is there a way to do that? Additionally, I want to "MARK" the initial value on both sliders so the user knows how they changed the slider from the INITIAL value.

library(shiny)

ui =pageWithSidebar(
  headerPanel("Glass fullness"),
  sidebarPanel(
    sliderInput(inputId = "Full", label = "% water", min = 0, max = 1, value = 0.2),
    sliderInput(inputId = "Empty", label = "% air", min = 0, max = 1, value = 1 - 0.2),
    uiOutput("Empty")),
  mainPanel()
)

server = function(input, output, session){
  
  # when water change, update air
  observeEvent(input$Full,  {
    updateSliderInput(session = session, inputId = "Empty", value = 1 - input$Full)
  })
  
  # when air change, update water
  observeEvent(input$Empty,  {
    updateSliderInput(session = session, inputId = "Full", value = 1 - input$Empty)
  })
  
}

shinyApp(ui = ui, server = server)

Solution

  • You may disable the other input (used shinyjs::disabled) and I have used the label of sliderInput to mark the initial value.

    library(shiny)
    library(shinyjs)
    
    initial_water <- 0.2
    initial_air <- 1 - initial_water
    
    ui = pageWithSidebar(
      headerPanel("Glass fullness"),
      sidebarPanel(
        useShinyjs(),
        sliderInput(inputId = "Full", 
                    label = paste('% water (initial value : ', initial_water, ')'), 
                    min = 0, max = 1, value = initial_water),
        shinyjs::disabled(
          sliderInput(inputId = "Empty", 
                      label = paste('% water (initial value : ', initial_air, ')'), 
                      min = 0, max = 1, value = initial_air)
        ),
        uiOutput("Empty")),
      mainPanel()
    )
    
    server = function(input, output, session){
      
      # when water change, update air
      observeEvent(input$Full,  {
        updateSliderInput(session = session, inputId = "Empty", value = 1 - input$Full)
      })
      
    }
    
    shinyApp(ui = ui, server = server)
    

    enter image description here