| 1 |
library(shiny) |
| 2 |
library(bslib) |
| 3 |
|
| 4 |
# Define UI for app that draws a histogram ---- |
| 5 |
ui <- page_sidebar( |
| 6 |
|
| 7 |
# App title ---- |
| 8 |
title = "Hello Shiny!", |
| 9 |
|
| 10 |
# Sidebar panel for inputs ---- |
| 11 |
sidebar = sidebar( |
| 12 |
|
| 13 |
# Input: Slider for the number of bins ---- |
| 14 |
sliderInput( |
| 15 |
inputId = "bins", |
| 16 |
label = "Number of bins:", |
| 17 |
min = 1, |
| 18 |
max = 50, |
| 19 |
value = 30 |
| 20 |
) |
| 21 |
), |
| 22 |
|
| 23 |
# Output: Histogram ---- |
| 24 |
plotOutput(outputId = "distPlot") |
| 25 |
) |
| 26 |
|
| 27 |
# Define server logic required to draw a histogram ---- |
| 28 |
server <- function(input, output) { |
| 29 |
|
| 30 |
# Histogram of the Old Faithful Geyser Data ---- |
| 31 |
# with requested number of bins |
| 32 |
# This expression that generates a histogram is wrapped in a call |
| 33 |
# to renderPlot to indicate that: |
| 34 |
# |
| 35 |
# 1. It is "reactive" and therefore should be automatically |
| 36 |
# re-executed when inputs (input$bins) change |
| 37 |
# 2. Its output type is a plot |
| 38 |
output$distPlot <- renderPlot({ |
| 39 |
x <- faithful$waiting |
| 40 |
bins <- seq(min(x), max(x), length.out = input$bins + 1) |
| 41 |
|
| 42 |
hist( |
| 43 |
x, |
| 44 |
breaks = bins, |
| 45 |
col = "#75AADB", |
| 46 |
border = "white", |
| 47 |
xlab = "Waiting time to next eruption (in mins)", |
| 48 |
main = "Histogram of waiting times" |
| 49 |
) |
| 50 |
}) |
| 51 |
} |
| 52 |
|
| 53 |
# Create Shiny app ---- |
| 54 |
shinyApp(ui = ui, server = server) |