Wednesday 28 December 2016

r - Access sliderInput range



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.


Answer



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


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...