Slider
#| standalone: true
#| components: [viewer]
#| viewerHeight: 200
library(shiny)
library(bslib)
ui <- page_fluid(
sliderInput("slider", "Slider",
min = 0, max = 100, value = 50),
verbatimTextOutput("value")
)
server <- function(input, output) {
output$value <- renderPrint({input$slider})
}
shinyApp(ui = ui, server = server)
library(shiny)
library(bslib)
ui <- page_fluid(
sliderInput("slider", "Slider",
min = 0, max = 100, value = 50),
verbatimTextOutput("value")
)
server <- function(input, output) {
output$value <- renderPrint({input$slider})
}
shinyApp(ui = ui, server = server)
Relevant Functions
-
sliderInput
sliderInput(inputId, label, min, max, value, step = NULL, round = FALSE, ticks = TRUE, animate = FALSE, width = NULL, sep = ",", pre = NULL, post = NULL, timeFormat = NULL, timezone = NULL, dragRange = TRUE)
-
animationOptions
animationOptions(interval = 1000, loop = FALSE, playButton = NULL, pauseButton = NULL)
Details
A slider is a widget that lets you drag to select a number, date, or date-time from a specified range.
To add a slider to your app:
Add
sliderInput()
to the UI of your app to create a slider. Where you call this function will determine where the slider will appear within the app’s layout.Specify the
inputId
andlabel
parameters ofsliderInput()
to define the identifier and label of the slider.Use the
min
andmax
parameters to define the minimum and maximum values of the slider.Set the
value
parameter to define the starting slider value.min
,max
, andvalue
can be numbers, dates, or date-times (POSIXct).
The value of an input component is accessible as a reactive value within the server()
function. To access the value of a slider:
- Use
input$<slider_id>
(e.g.,input$slider
) to access the value of the slider. The server value of a slider is a number, date, or date-time (depending on the class ofvalue
).
See also
Variations
Animated Slider
To provide a play button alongside the slider, set the animate
parameter to TRUE
. When a user presses the play button, the slider will iterate over its range of values. Set animate
to the animationOptions()
helper to customize the animation behavior.
library(shiny)
library(bslib)
ui <- page_fluid(
sliderInput(
"slider", "Slider", min = 0, max = 100, value = 50,
animate = animationOptions(interval = 100, loop = TRUE)),
verbatimTextOutput("value")
)
server <- function(input, output) {
output$value <- renderPrint({input$slider})
}
shinyApp(ui = ui, server = server)