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