闪亮的滑块输入从最大值到最小值 [英] shiny sliderInput from max to min

查看:18
本文介绍了闪亮的滑块输入从最大值到最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

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

这篇关于闪亮的滑块输入从最大值到最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆