r shiny sliderInput with exact values instead of evenly divided range

Viewed 335

So the API of the sliderInput widget allows you to set a min and a max and then it gives you a range between the min and max where the range is evenly divided. Is it possible to give sliderInput a vector of all the values you want and have the slider only able to land in one of those values in the vector?

1 Answers

I think the sliderTextInput from shinyWidgets does what you want. Though on the slider, all values are equally separated and not proportionnally.

library(shiny)
library(shinyWidgets)

ui <- fluidPage(
  
  sliderTextInput(
    inputId = "myslider",
    label = "Choose a value:", 
    choices = c(2,3,5,7,11,13,17,19,23,29,31),
    grid = TRUE
  )
)

server <- function(input, output, session) {
  observe(print(input$myslider))
}

shinyApp(ui, server)
Related