Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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) {}
  )
 )
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
640 views
Welcome To Ask or Share your Answers For Others

1 Answer

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]
      })
    }
  )
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...