1
votes

I'm having trouble with sliderInput rounding my starting value parameter when the min/max range is large. How can I prevent sliderInput from rounding my starting value parameter in cases like the example below?

library(shiny)

ui <- fluidPage(
    sliderInput(
      inputId = "test",
      label = "A Nice Label",
      min = 2.52,
      max = 45734.68,
      value = 30982.63, # auto rounded to nearest integer in application, which I do not want it to do.
      round = FALSE # tried this argument, no dice.
    ),
    verbatimTextOutput(
      "value"
    )
  )

## SERVER PROCESSING DEFINITION
# this section defines how data is processed in response to manipulations in the ui
server <- function(input, output, session) {

  output$value <- renderText({
    input$test
  }) 

}

shinyApp(ui, server)
1

1 Answers

1
votes

You need to supply the step = argument to increase precision arbitrarily. The documentation says "a heuristic is used" to determine step size by default, and that leads to the rounding you observed. See this updated version:

library(shiny)

ui <- fluidPage(
  fluidPage(
    sliderInput(
      inputId = "test",
      label = "A Nice Label",
      min = 2.52,
      max = 45734.68,
      step = 0.01,      # the smallest level of precision in your args
      value = 30982.63, # no longer rounded
      round = FALSE     # this is the default
    ),
    verbatimTextOutput(
      "value"
    )
  )
)

## SERVER PROCESSING DEFINITION
# this section defines how data is processed in response to manipulations in the ui
server <- function(input, output, session) {

  output$value <- renderText({
    sprintf("%5.6f", input$test)       # format text to show precision
  }) 

}

shinyApp(ui, server)

If you dig into sliderInput you'll find the following definition for shiny:::findStepSize:

function (min, max, step) 
{
  if (!is.null(step)) 
    return(step)
  range <- max - min
  if (range < 2 || hasDecimals(min) || hasDecimals(max)) {
    pretty_steps <- pretty(c(min, max), n = 100)
    n_steps <- length(pretty_steps) - 1
    signif(digits = 10, (max(pretty_steps) - min(pretty_steps))/n_steps)
  }
  else {
    1
  }
}

So you can see that it tries to have about 100 steps between your min and max, so they are automatically rounded to integers (or even larger steps).