Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
593 views
in Technique[技术] by (71.8m points)

r - shiny sliderInput from max to min

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

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

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...