| 1 |
library(shiny) |
| 2 |
library(bslib) |
| 3 |
|
| 4 |
# Define UI for slider demo app ---- |
| 5 |
ui <- page_sidebar( |
| 6 |
|
| 7 |
# App title ---- |
| 8 |
title = "Sliders", |
| 9 |
|
| 10 |
# Sidebar panel for inputs ---- |
| 11 |
sidebar = sidebar( |
| 12 |
|
| 13 |
# Input: Simple integer interval ---- |
| 14 |
sliderInput( |
| 15 |
"integer", |
| 16 |
"Integer:", |
| 17 |
min = 0, |
| 18 |
max = 1000, |
| 19 |
value = 500 |
| 20 |
), |
| 21 |
|
| 22 |
# Input: Decimal interval with step value ---- |
| 23 |
sliderInput( |
| 24 |
"decimal", |
| 25 |
"Decimal:", |
| 26 |
min = 0, |
| 27 |
max = 1, |
| 28 |
value = 0.5, |
| 29 |
step = 0.1 |
| 30 |
), |
| 31 |
|
| 32 |
# Input: Specification of range within an interval ---- |
| 33 |
sliderInput( |
| 34 |
"range", |
| 35 |
"Range:", |
| 36 |
min = 1, |
| 37 |
max = 1000, |
| 38 |
value = c(200, 500) |
| 39 |
), |
| 40 |
|
| 41 |
# Input: Custom currency format for with basic animation ---- |
| 42 |
sliderInput( |
| 43 |
"format", |
| 44 |
"Custom Format:", |
| 45 |
min = 0, |
| 46 |
max = 10000, |
| 47 |
value = 0, |
| 48 |
step = 2500, |
| 49 |
pre = "$", |
| 50 |
sep = ",", |
| 51 |
animate = TRUE |
| 52 |
), |
| 53 |
|
| 54 |
# Input: Animation with custom interval (in ms) ---- |
| 55 |
# to control speed, plus looping |
| 56 |
sliderInput( |
| 57 |
"animation", |
| 58 |
"Looping Animation:", |
| 59 |
min = 1, |
| 60 |
max = 2000, |
| 61 |
value = 1, |
| 62 |
step = 10, |
| 63 |
animate = |
| 64 |
animationOptions(interval = 300, loop = TRUE) |
| 65 |
) |
| 66 |
), |
| 67 |
|
| 68 |
# Output: Table summarizing the values entered ---- |
| 69 |
tableOutput("values") |
| 70 |
) |
| 71 |
|
| 72 |
# Define server logic for slider examples ---- |
| 73 |
server <- function(input, output) { |
| 74 |
|
| 75 |
# Reactive expression to create data frame of all input values ---- |
| 76 |
sliderValues <- reactive({ |
| 77 |
data.frame( |
| 78 |
Name = c( |
| 79 |
"Integer", |
| 80 |
"Decimal", |
| 81 |
"Range", |
| 82 |
"Custom Format", |
| 83 |
"Animation" |
| 84 |
), |
| 85 |
Value = as.character(c( |
| 86 |
input$integer, |
| 87 |
input$decimal, |
| 88 |
paste(input$range, collapse = " "), |
| 89 |
input$format, |
| 90 |
input$animation |
| 91 |
)), |
| 92 |
stringsAsFactors = FALSE |
| 93 |
) |
| 94 |
}) |
| 95 |
|
| 96 |
# Show the values in an HTML table ---- |
| 97 |
output$values <- renderTable({ |
| 98 |
sliderValues() |
| 99 |
}) |
| 100 |
} |
| 101 |
|
| 102 |
# Create Shiny app ---- |
| 103 |
shinyApp(ui, server) |