Search code examples
rshinyroundinguislider

r shiny slider input round


I have a problem with the R shiny slider input. The "Round feature" does not work as you can see in in this picture. Did I do something wrong ?

  sliderInput("Er", "Choose expected return (in percent)",
              min = min, max = max, value = min , round = -1,
              sep = "" , post = "%", ticks = FALSE
          )

Solution

  • You have to specify a step for rounding to work:

    library(shiny)
    min_Er <- 20.31
    max_Er <- 23.59
    shinyApp( ui = fluidPage(sliderInput("Er1", "Rounding doesn't work", 
                                         round = -2, step = NULL,
                                         min = min_Er, 
                                         max = max_Er,
                                         value = min_Er,
                                         sep = "" , post = "%", ticks = FALSE),
    
                             sliderInput("Er2", "Rounding works",  
                                         round = -2, step = 0.01,
                                         min = min_Er, 
                                         max = max_Er,
                                         value = min_Er,
                                         sep = "" , post = "%", ticks = FALSE)
    ), server=function(input, output, session){
      observe(print(input$Er1))
      observe(print(input$Er2))
    })
    

    enter image description here

    Otherwise, as commented by @Ryan Morton, if you use integers for min and max, rounding will work even if step = NULL:

    library(shiny)
    min_Er <- 20.31
    max_Er <- 23.59
    shinyApp(ui = fluidPage(sliderInput("Er1", "Rounding doesn't work", 
                                        round = TRUE,
                                        min = min_Er, 
                                        max = max_Er,
                                        value = min_Er, 
                                        sep = "" , post = "%", ticks = FALSE),
    
                            sliderInput("Er2", "Rounding works",  
                                        round = TRUE,
                                        min = floor(min_Er), 
                                        max = ceiling(max_Er),
                                        value = min_Er,
                                        sep = "" , post = "%", ticks = FALSE)
    ), server=function(input, output, session){
      observe(print(input$Er1))
      observe(print(input$Er2))
    })
    

    enter image description here