1

I have an issue with sliderInput. In my app, when a user loads data (actionBTTN) the years of my sliderInput get updated (with updateSliderInput) according to that data. But I have noticed that for data containing few years (4-5) the sliderInput would show repeated years. I also noticed, that it seems to only happens when my screen size is bigger.

Here is how it looks with a small sized window: no duplicated year values, how I would like it to be in the app And here is how it looks with a larger window: duplicated year values

Here is an example of my app:


library(shiny)
library(shinyWidgets)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      # initial input with no years selected, it will update once data is loaded
      sliderInput(
        inputId = "years", 
        label = "Choose a time range:", 
        min = 0, max = 1, 
        value = c(0,1), 
        #dragRange = TRUE, 
        step = 1, 
        sep = ''
      ),
      actionBttn(
        inputId = "load_data",
        label = "Get data!",
        color = "primary",
        style = "fill", 
        icon = icon("arrow-alt-circle-down"),
        size = "xs",
        block = TRUE
      )
    ),
    mainPanel()
  )
)

server = function(input, output, session) {
  observeEvent(input$load_data, {
    mydata <- data.frame(numbers = c(1:5), year = c(2010:2014))
    
    minim = min(mydata$year)
    maxim = max(mydata$year)
    
    updateSliderInput(session, inputId = "years",
                      min = minim, max = maxim, 
                      value = c(minim, maxim),
                      # calculate step dynamically (@Isa's idea)
                      step = seq(minim, maxim, length.out = length(mydata$year))[2] - seq(minim, maxim, length.out = length(mydata$year))[1])
  })
}

shinyApp(ui, server)

I have already tried changing the step and sep arguments, also seting mydata$year as integer. Has anybody encountered the same issue? All help is welcome.

Thanks :)

  • This may be a bug in `updateSliderInput()`, as noted in issue #[2398](https://github.com/rstudio/shiny/issues/2398) – YBS Dec 10 '21 at 17:53
  • Thanks! I'll watch it. But as it was reported in 2019 I don't think they are currently working on it. But good to know! – Claudia Meneses Dec 13 '21 at 09:05
  • Please try the workaround by alandipert in that link. – YBS Dec 13 '21 at 12:31

1 Answers1

2

Maybe this is what you were expecting

mydata <- data.frame(numbers = c(1:6), year = c(2010:2015))

library(shiny)
library(shinyWidgets)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      # initial input with no years selected, it will update once data is loaded
      sliderInput(
        inputId = "years", 
        label = "Choose a time range:", 
        min=0, max=1, 
        value = 0, 
        dragRange = TRUE, 
        step = seq(0, 1, length.out = length(mydata$year))[2] - seq(0, 1, length.out = length(mydata$year))[1], 
        # step = 0.2, # Above step calculates dynamically
        sep = ''
      ),
      actionBttn(
        inputId = "load_data",
        label = "Get data!",
        color = "primary",
        style = "fill", 
        icon = icon("arrow-alt-circle-down"),
        size = "xs",
        block = TRUE
      )
    ),
    mainPanel()
  )
)

server = function(input, output, session) {
  observeEvent(input$load_data, {
    min = min(mydata$year)
    max = max(mydata$year)

    updateSliderInput(session, inputId = "years",
                      min = min, max = max, 
                      value = min, step = 1)
  })
}

shinyApp(ui, server)

Reason behind updateSliderInput repeating the values was to fill the number of ticks generated by sliderInput in ui object when argument ticks is passed as TRUE.

Behind the scene code of sliderInput for number of ticks:

range <- max - min
if (ticks) {
    n_steps <- range/step
    scale_factor <- ceiling(n_steps/10)
    n_ticks <- n_steps/scale_factor
}
else {
    n_ticks <- NULL
}

You can check the whole working of any function by keeping the cursor on function and pressing f2 in windows.

So if we want the values not to repeat then number of ticks should be equal to number of distinct values of the variable in consideration.

Isa
  • 496
  • 3
  • 6
  • Hi @Isa, Thanks! This helped a lot. the issue is that `mydata` gets loaded after a selection, so it is only available "at the server side" (I have my data in `reactiveVlaues`). I edited my code to represent that. I calculated the steps as you did, but within the `updateSliderInput` and it doesn't work. The ticks do not correctly mark the years – Claudia Meneses Dec 13 '21 at 09:01