Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shiny sliderInput from max to min

Tags:

r

slider

shiny

Is it possible to make a sliderInput that shows the values in decreasing order (from left to right; eg. 5 4 3 2 1)?

runApp(
 list(
  ui = fluidPage(
   sliderInput("test","", min=5, max=1, value = 3, step=1)
   ),
   server = function(input,output) {}
  )
 )
like image 459
samssan Avatar asked Mar 14 '23 20:03

samssan


1 Answers

EDIT 2017-10-13: This function is now available in package shinyWidgets (with a different name : sliderTextInput()).

Hi you can write your own slider function like this (it's a little dirty...) :

sliderValues <- function (inputId, label, values, from, to = NULL, width = NULL) {
        sliderProps <- shiny:::dropNulls(list(class = "js-range-slider", 
                                      id = inputId,  
                                      `data-type` = if (!is.null(to)) "double",
                                      `data-from` = which(values == from) - 1,
                                      `data-to` = if (!is.null(to)) which(values == to) - 1,
                                      `data-grid` = TRUE,
                                      `data-values` = paste(values, collapse = ", ")
                                      ))
        sliderProps <- lapply(sliderProps, function(x) {
          if (identical(x, TRUE)) 
            "true"
          else if (identical(x, FALSE)) 
            "false"
          else x
        })
        sliderTag <- div(class = "form-group shiny-input-container", 
                         style = if (!is.null(width)) 
                           paste0("width: ", validateCssUnit(width), ";"), 
                         if (!is.null(label)) 
                           shiny:::controlLabel(inputId, label), do.call(tags$input, 
                                                                 sliderProps))
        dep <- list(htmltools::htmlDependency("ionrangeslider", "2.0.12", c(href = "shared/ionrangeslider"), 
                                   script = "js/ion.rangeSlider.min.js",
                                   stylesheet = c("css/ion.rangeSlider.css",
                                                  "css/ion.rangeSlider.skinShiny.css")))
        htmltools::attachDependencies(sliderTag, dep)
      }

The point to do this is to use the values attribute from ionrangeslider (see section Using custom values array here)

The downside is the value of the input you retrieve server-side isn't the value of the slider but the index of the value (starting from 0).

You can use this function like this :

library("shiny")
runApp(
  list(
    ui = fluidPage(
      # you have to pass the values you want in the slider directly to th function
      sliderValues(inputId = "test", label = "", from = 5, values = 5:1),
      verbatimTextOutput(outputId = "slidervalue")
    ),
    server = function(input,output) {
      output$slidervalue <- renderPrint({
        # Careful ! : input$test isn't the expected value !!!
        (5:1)[input$test + 1]
      })
    }
  )
)

And bonus : it works with characters vectors too :

runApp(
  list(
    ui = fluidPage(
      sliderValues(inputId = "test", label = "", from = "g", to = "o", values = letters),
      verbatimTextOutput(outputId = "slidervalue")
    ),
    server = function(input,output) {
      output$slidervalue <- renderPrint({
        # Careful ! : input$test isn't the expected value !!!
        letters[input$test + 1]
      })
    }
  )
)
like image 57
Victorp Avatar answered Mar 23 '23 12:03

Victorp