sliderInput最大/最小文本标签

时间:2016-11-04 04:48:22

标签: r shiny shinydashboard

我目前正在使用具有多个sliderInput的仪表板。是否可以用文本替换max和min标签?例如,我的min = 1和max = 10.我想保持比例从1到10,同时滑块标签分别显示为“更快”和“更晚”。

谢谢!

4 个答案:

答案 0 :(得分:2)

这里简短的答案是不可悲的;如果没有浏览JavaScript中的底层代码,就无法重新标记滑块刻度(尽管有一些格式化参数)。

但是,可以通过传递一个使用内联CSS设置样式的HTML对象来将标签破解到窗口小部件标签中。确保设置小部件本身的宽度,以便一切排好,结果也不错:

library(shiny)

ui <- fluidPage(

    sliderInput(inputId = 'slider', 
                label = div(style='width:300px;', 
                            div(style='float:left;', 'sooner'), 
                            div(style='float:right;', 'later')), 
                min = 0, max = 10, value = 5, width = '300px')

    )

server <- function(input, output) {

}

shinyApp(ui, server)

labeled slider

答案 1 :(得分:1)

也许有点像这样。请注意,滑块将返回从零开始的索引。

app.R

    library(shiny)

    df <- data.frame(x=1:24)


    ui <- tagList(
            tags$head(
                    HTML("<script type='text/javascript' src='js/sliderInit.js'></script>")
            ),
            fluidPage(


       titlePanel("Custom firs and last label"),


       sidebarLayout(
          sidebarPanel(
             sliderInput("hour",
                         "Select hour:",
                         min = 1,
                         max = 10,
                         value = 5)
          ),


          mainPanel(
             textOutput("selectedNumber")
          )
       )
    ))


    server <- function(input, output) {

       output$selectedNumber <- renderText({
               df[input$hour+1,]
       })
    }


    shinyApp(ui = ui, server = server)

sliderInit.js

$(document).ready(function() {
    $("#hour").ionRangeSlider({
            values: ["Sooner", "2", "3", "4", "5", "6", "7", "8", "9", "Later"]
    });
    });

答案 2 :(得分:1)

这可以通过操纵ionRangeSlider的prettify函数来完成,该函数返回可选滑块值范围内每个值的滑块标签。只需创建一个文件slider.js,如下所示(在标记的位置输入您的滑块ID和最低可选值)并将其包含在您的应用中includeScript('slider.js')

$(document).ready(function() {
  /**
    Custom slider labels
  **/

    // Convert numbers of min to max to "lower" and "higher"
    function returnLabels(value) {
      // remove label of selected
      $('.my_slider').find('.irs-single').remove();
    //  $('.my_slider').find('.irs-grid-text').remove(); // this is an alternative to ticks=F

      if (value === 0){ // enter your lowest slider value here
        return "lower";
      }else{
        return "higher";
      }
    }

    var someID = $("#YOUR_SLIDER_ID").ionRangeSlider({ // enter your shiny slider ID here
          prettify: returnLabels,
          force_edges: true,
          grid: false
        });
    });

然后,将滑块包含在带有“my_slider”类的div中:

library(shiny)    
ui <- fluidPage(
  includeScript("slider.js"),
  div(class="my_slider", # to be able to manipulate it with JQuery
      sliderInput("YOUR_SLIDER_ID",
                  "Slider Value:", ticks = F,
                  min = 0, max = 50, value = 30))      
)

server <- function(input, output) {}    
shinyApp(ui, server)

结果如下:

enter image description here

要修复选择最小/最大值时标签消失的错误,请在您的应用中定义此UI输出:

# Just a small fix to reactivate Labels when Min/Max value is chosen
output$fixSlider <- renderUI({     
 # declare dependency on slider
 input$YOUR_SLIDER_ID     
 minVis <- "$('.my_slider').find('.irs-min').attr('style','visibility: visible');"
 maxVis <- "$('.my_slider').find('.irs-max').attr('style','visibility: visible');"  
 return(tags$script(paste(minVis,maxVis)))
})

欢迎您: - )

答案 3 :(得分:0)

我遇到了同样的问题。不幸的是,Shiny和ionRangeSlider都没有提供这种更改的简单功能。在寻找一种纯粹顽固的微妙方式后,我遇到了以下的CSS黑客攻击。 (我还用JS隐藏了值游标。)

ui <- fluidPage(

tags$head(
    #Using ionRangeSlider's javascript options you can hide/show selector labels and min/max labels
    HTML("
    <script>
    $(document).ready(function(){
        $(\".js-range-slider\").ionRangeSlider({
        hide_min_max: false,
        hide_from_to: true
        });

    });

    </script>
    ")
),

#This CSS hack first hides the text within the span tags of the specified classes
#Then it adds desired text and CSS properties. !important parameter is to override
#inline css parameters.
tags$style(type = "text/css", "
    .irs-min {visibility:hidden !important;}
    .irs-max {visibility:hidden !important;}
    .js-irs-0 .irs .irs-min:after {content:'Hello' !important;}
    .js-irs-0 .irs .irs-max:after {content:'Shiny' !important;}
    //Restore css defaults to .irs-min and .irs-max
    .irs-min:after {
        visibility: visible !important;
        display: block;
        background: rgba(0, 0, 0, 0.1) none repeat scroll 0 0;
        border-radius: 3px;
        color: #333;
        font-size: 10px;
        line-height: 1.333;
        padding: 1px 3px;
        text-shadow: none;
        top: 0;
        cursor: default;
        display: block;
        left: 0;
        position: absolute;}

    .irs-max:after {
        visibility: visible !important;
        display: block;
        background: rgba(0, 0, 0, 0.1) none repeat scroll 0 0;
        border-radius: 3px;
        color: #333;
        font-size: 10px;
        line-height: 1.333;
        padding: 1px 3px;
        text-shadow: none;
        top: 0;
        cursor: default;
        display: block;
        right: 0;
        position: absolute;}

"),

sliderInput(inputId="test", label=NULL, min=1, max=10, value=5, step = 1, width='100%')

)

 server <- function(input, output, session){

   }

shinyApp(ui = ui, server=server)