Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining a specific range of values for a sliderInput in Shiny

Tags:

r

shiny

Even thought there are many topics dealing with the properties of the sliderInput in Shiny, I couldn't find a solution to what I'm trying to do.

My problem is simple: I would like to adapt my code (see below) in order to create a slider with an interval of "10" (from 0 to 10, 10 to 20, 3 to 13 etc...)

###running example: 

ui <-basicPage(
  sliderInput("id", "Ranking",
                           min = 2, max = 60, value = c(2,15)))


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

shinyApp(ui, server)

Any ideas please ?

like image 938
Linda Avatar asked Oct 22 '25 14:10

Linda


1 Answers

You can call updateSliderInput every time the slider is changed, to enforce the interval.

The trick here is to remember previous value to update the right handler (the one that that didn't change)

INTERVAL = 13
value = c(2, 2 + INTERVAL)

ui <-basicPage(
  sliderInput("id", "Ranking",
              min = 2, max = 60, value = value))

server <- server <- function(input, output, session) {
  observeEvent(input$id,{
    newvalue = input$id
    
    if(value[1] != newvalue[1] && newvalue[2] - newvalue[1] != INTERVAL)
      updateSliderInput(session, "id", value = c(newvalue[1], newvalue[1] + INTERVAL))
    
    if(value[2] != newvalue[2] && newvalue[2] - newvalue[1] != INTERVAL)
      updateSliderInput(session, "id", value = c(newvalue[2] - INTERVAL, newvalue[2]))
    
    value <<- newvalue
  })
}

shinyApp(ui, server)
like image 75
HubertL Avatar answered Oct 25 '25 05:10

HubertL



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!