Search code examples
rshinynouislidershinywidgets

Is there a possibility to update the step parameter in noUiSliderInput or to make it depend on min and max?


Is there a possibility to update the step parameter in noUiSliderInput or to make it depend on min and max. It would also work to have a possibility to set that we want 10 steps for instance. Thanks.

Here is a reproducible example inspired from the updateNoUiSliderInput demo:

ui <- fluidPage(
  tags$h3("Update method"),
  tags$br(),
  
  fluidRow(
      panel(
        status = "danger", heading = "Update min/max",
        noUiSliderInput(
          inputId = "to_update_minmax",
          label = "Slider disable:",
          min = 0, max = 100, value = 50,
          colo = "#F2DEDE", 
          step = 10
        ),
        verbatimTextOutput(outputId = "res_update_minmax"),
        actionButton(inputId = "minmax_0_100", label = "Set min=0 & max=100"),
        actionButton(inputId = "minmax_1000_5000", label = "Set min=1000 & max=5000")
      ),
    )
  )
)

server <- function(input, output, session) {
  
  output$res_update_minmax <- renderPrint(input$to_update_minmax)
  observeEvent(input$minmax_0_100, {
    updateNoUiSliderInput(
      session = session,
      inputId = "to_update_minmax",
      range = c(0, 100), 
      step = 10 # this is not a valid argument for updateNoUiSliderInput !!!
    )
  })
  observeEvent(input$minmax_1000_5000, {
    updateNoUiSliderInput(
      session = session,
      inputId = "to_update_minmax",
      range = c(1000, 5000)
      step = 500 # this is not a valid argument for updateNoUiSliderInput !!!
    )
  })
  
}


shinyApp(ui, server)

It would also work with something like writing in the original noUiSliderInput: step = (max - min) / 10 but with min and max being the curent max values of the input or having a argument to set automatically the steps number and not the steps distance.

Thanks.


Solution

  • This will do what you want:

    library(shiny)
    library(shinyWidgets)
    ui <- fluidPage(
        tags$h3("Update method"),
        tags$br(),
        
        fluidRow(
            panel(
                status = "danger", heading = "Update min/max",
                uiOutput("to_update_minmax_wrapper"),
                verbatimTextOutput(outputId = "res_update_minmax"),
                actionButton(inputId = "minmax_0_100", label = "Set min=0 & max=100"),
                actionButton(inputId = "minmax_1000_5000", label = "Set min=1000 & max=5000")
            )
        )
    )
    
    server <- function(input, output, session) {
        
        output$res_update_minmax <- renderPrint(input$to_update_minmax)
        output$to_update_minmax_wrapper <- renderUI({
            noUiSliderInput(
                inputId = "to_update_minmax",
                label = "Slider disable:",
                min = 0, max = 100, value = 50,
                colo = "#F2DEDE", 
                step = (100 - 0)/10
            )
        })
        
        observeEvent(input$minmax_0_100, {
            output$to_update_minmax_wrapper <- renderUI({
                noUiSliderInput(
                    inputId = "to_update_minmax",
                    label = "Slider disable:",
                    min = 0, max = 100, value = 50,
                    colo = "#F2DEDE", 
                    step = (100 - 0)/10
                )
            })
        })
        observeEvent(input$minmax_1000_5000, {
            output$to_update_minmax_wrapper <- renderUI({
                noUiSliderInput(
                    inputId = "to_update_minmax",
                    label = "Slider disable:",
                    min = 1000, max = 5000, value = mean(c(5000, 1000)),
                    colo = "#F2DEDE", 
                    step = (5000 - 1000)/10
                )
            })
        })
        
    }
    
    
    shinyApp(ui, server)
    
    

    You are right step is not valid in updateNoUiSliderInput, but we can use renderUI to create a new one so you can use step.