Customize labels on sliderInput

6,193 views
Skip to first unread message

Phil Brockman

unread,
Aug 14, 2014, 10:20:20 AM8/14/14
to shiny-...@googlegroups.com
Hello,

I'm working on a project where I have this line of code in the UI.R sliderInput("ps", "Select point size", min=.5,value=1, max=1.5, step=.1).  Instead of having ".5" on the upper left and "1.5" on the upper right, is there a way that I can change these to "small" and "large" respectively?  It would also be nice if I could label the tick mark in the middle as "middle" as well, but having the text on the extremes is my primary goal.

Thank you!

Sigbert Klinke

unread,
Aug 20, 2014, 3:07:35 AM8/20/14
to shiny-...@googlegroups.com
For slider with a logarithmic scale I used

    ticks <- ticks(length(var), nmin=30)
    sliderInput
(inputId   = "size",
                label    
= "Sample size (n)",
                min      
= 1,
                max      
= length(ticks),
                value    
= 1,
                ticks    
= ticks,
                step      
= 1)

The ticks are shown below the slider. Above it will still show 1...length(ticks) and also input$size will have the values 1...length(ticks). To get the correct sample size I have to access ticks[input$size]. Best Sigbert

Message has been deleted

Sigbert Klinke

unread,
Feb 18, 2015, 8:06:53 AM2/18/15
to shiny-...@googlegroups.com
UPDATE: This does not work anymore with Shiny 0.11.x and creates the warnings

Warnung in if (ticks) { :
  Bedingung hat L?nge > 1 und nur das erste Element wird benutzt
Warnung in if (!is.na(attribValue)) { :
  Bedingung hat L?nge > 1 und nur das erste Element wird benutzt

However, since Ion.RangeSlider provides a values element for custom labels you can manipulate the input element from R directly and it return it to shiny:

 ticks  <- c(90, 95, 99, 99.9)
 values
<- paste0(' data-values="', paste0(ticks, collapse=','), '"/>')
 html  
<- sliderInput("foo", min=1, max=length(ticks), ticks=T)
 html  
<- sub('/>', values, html, fixed=T)
 html

Note: Setting ticks to TRUE or FALSE does not change the behaviour of the slider. It changes only if all the values are shown under the slider or not. The only limitation seems that elements in ticks should not contain commas since Ion.RangeSlider uses split(",") to decompose the data-values element.

mbh

unread,
Feb 23, 2015, 12:25:27 PM2/23/15
to shiny-...@googlegroups.com
Hello Sigbert,

I tried your code but I wasn't able to get it work correctly. The surprising thing is that it's working when I remove the "#" in the commented line.

ui <- pageWithSidebar(
  headerPanel("Slider labels"),
  sidebarPanel(
#     sliderInput("foo2", label = "slider 1 :", min = 1, max = 4, value = 1),
    uiOutput("slider")
  ),
  mainPanel()
)

server <- function(input, output) {
  output$slider <- renderUI ({
    ticks  <- c(90, 95, 99, 99.9)
    values <- paste0(' data-values="', paste0(ticks, collapse=','), '"/>')
    html   <- sliderInput("foo", label = "slider 2:", min=1, max=length(ticks), ticks=T, value = 1)
    html   <- sub('/>', values, html, fixed=T) 
    html
  })
}
runApp(list(ui = ui, server = server))

Thanks for your help,
Matt

Winston Chang

unread,
Feb 23, 2015, 3:16:11 PM2/23/15
to mbh, shiny-discuss
Sigbert - Could you file an issue about custom ticks labels in the Shiny issue tracker?

Also, your code doesn't work for me (and I also wouldn't expect it to work, since it's trying to modify the text of an object that isn't actually text).

-Winston

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/99d0b375-3951-4e80-83a0-11001a825e95%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Sigbert Klinke

unread,
Feb 24, 2015, 3:14:49 AM2/24/15
to shiny-...@googlegroups.com, matthie...@bluestone.fr
Hi,

yep, in another example my code did not work too. I did yesterday evening a modified version, but I still have to try on my first example and to make it a bit more secure.

  output$sizeUI <- renderUI({
    args      
<- mmstat$UI$size  # fixed parameters for sliderInput stored in a list
    args$min  
<- 1
    args$max  
<- length(args$ticks)
   
if (sessionInfo()$otherPkgs$shiny$Version=="0.11") {
      ticks
<- paste0(args$ticks, collapse=',')
      args$ticks
<- T
      html  
<- do.call('sliderInput', args)
      html$children
[[2]]$attribs[['data-values']] <- ticks;
   
} else {
      html  
<- do.call('sliderInput', args)    
   
}
    html
 
})



Sigbert Klinke

unread,
Feb 24, 2015, 2:26:06 PM2/24/15
to shiny-...@googlegroups.com, matthie...@bluestone.fr
Hi,

that is what I'am going to use for shiny 0.10.4 and 0.11:

mmstat.sliderInput <- function (...) {
  iapply
<- function (l) {
    ret
<- l
   
if (is.list(l)) {
     
if (!is.null(ret$name) && (ret$name=='input')) {
        ret$attribs
[['data-values']] <- ticks
     
} else {
       
for (i in seq(ret)) ret[[i]] <- iapply(ret[[i]])
     
}
   
}
    ret
 
}
 
  args
<- list(...)
 
if ((compareVersion(mmstat$shiny, "0.11")>=0) && !is.null(args$ticks) && length(args$ticks)) {

    ticks      
<- paste0(args$ticks, collapse=',')
    args$ticks
<- T
    html      
<- do.call('sliderInput', args)

    html      
<- iapply(html)  
 
} else {

    html      
<- do.call('sliderInput', args)
 
}
  html
}

Basically iapply looks (recursively) for a list element with ...$name=='input' and then adds a  ...$attribs[['data-values']] element.And I call it by

  output$sizeUI <- renderUI({
    mmstat
.log('sizeUI')
    args      
<- mmstat$UI$size
    args$min  
<- 1
    args$max  
<- length(args$ticks)
   
do.call('mmstat.sliderInput', args)
 
})

Maybe the code could be simplified/elegant by an R guru.

mbh

unread,
Mar 3, 2015, 5:50:28 AM3/3/15
to shiny-...@googlegroups.com, matthie...@bluestone.fr
Thank you Sigbert.

Here is a solution for my example :

ui <- pageWithSidebar(
  headerPanel
("Slider labels"),
  sidebarPanel
(

    uiOutput
("slider")
 
),
  mainPanel
()
)

server
<- function(input, output) {
  output$slider
<- renderUI({

      args      
<- list(inputId="foo", label="slider :", ticks=c(90,95,99,99.9), value=c(2,3))

      args$min  
<- 1
      args$max  
<- length(args$ticks)

     
if (sessionInfo()$otherPkgs$shiny$Version>="0.11") {

        ticks
<- paste0(args$ticks, collapse=',')
        args$ticks
<- T
        html  
<- do.call('sliderInput', args)

        html$children
[[2]]$attribs[['data-values']] <- ticks;

     
} else {
        html  
<- do.call('sliderInput', args)    
     
}

      html
   
})

}

runApp
(list(ui = ui, server = server))
Reply all
Reply to author
Forward
0 new messages