0
votes

In a shiny app I am currently developing the server regularly updates the min/max values of a sliderInput using updateSliderInput. For follow up calculations on the server I need to know what the current min/max values of the slider are. Is there any possibility in shiny to access the current min/max values of a sliderInput object (as symbolized by input$slider$max in the example below)?

library(shiny)

ui <- fluidPage(
      mainPanel(
         sliderInput("slider","Slider:", min = 1, max = 50, value = 30),
         selectInput("input","Input:",choices = c("a","b","c"))
      )
)

server <- function(input, output, session) {

  updateSliderInput(session,"slider",max=round(abs(rnorm(1)*100)))
  observe(print(input$slider$max))

}

shinyApp(ui = ui, server = server)

I am aware that I could introduce a separate variable tracking the status of the min/max settings, but (if possible) would like to avoid that options as it bears the risk of getting out of sync between actual settings and settings stored in the tracking variable.

edit: I am looking for the min/max settings set in sliderInput or updateSliderInput, not for the min/max range a user might choose. Moving the slider to read min and max is (if possible) also not an option as I need to get this information without updating the slider.

2
If my last edit helped you, accept the answer: stackoverflow.com/help/accepted-answerJ0ki

2 Answers

1
votes

It is possible using jQuery and the shinyjs package (to execute the jQuery). You need to write a custom function that retrieves the currently set max value (getMax in below example). If your slider is wrapped in a div with a fixed title mySlider, you can let jQuery find that slider:

$('#mySlider .irs-max').text());

returns the currently set max. I use a button to execute that function and alert the currently set max:

library(shiny)
library(shinyjs)

jQuery_max_code <- "shinyjs.getMax = function(){alert($('#mySlider .irs-max').text());}"

ui <- fluidPage(
  mainPanel(

    # make the function getMax() available to the browser
    useShinyjs(), 
    extendShinyjs(text=jQuery_max_code),

    # providing the slider an id so I can access it with jQuery: mySlider
    div(id = "mySlider", sliderInput("mySlider","Slider:", min = 1, max = 50, value = 30)),

    # a slider to change the max of our slider
    sliderInput("maxSlider","Max Value of above slider:", min = 1, max = 200, value = 30),

    # a button to alert the current max of mySlider
    actionButton("button", "Print slider maximum value")
  )
)

server <- function(input, output, session) {

  # change value of mySlider 
  observeEvent(input$maxSlider, {
    updateSliderInput(session, "mySlider", max=input$maxSlider)
  })

  # alert the current max after button click
  observeEvent(input$button, {
    js$getMax()
  })
}

shinyApp(ui = ui, server = server)

enter image description here

2
votes

If you want to take the max and min values you will need the position of them at the sliderinput so:

The min value will be input$slider[1] and the max value will be input$slider[2] or input$slider to get the current value that you choose.

Hope it helps.

Edit:

If you want to get or set the max or min value just do the updateSliderInput and change it by your own. My answer is about to get the max and min value that you choose in a slider. Otherwise you can move the value to the final and it will returns the max value or move it to the beginning and it will returns the min value using:

observe({
    print(input$slider)
    })

Edit 2:

Maybe that's what you want to do:

server <- function(input, output, session) {
  slidermax <- round(abs(rnorm(1)*100))
  updateSliderInput(session,"slider",max=slidermax)
  observe(print(slidermax))

}