| 1 |
library(shiny) |
| 2 |
|
| 3 |
# Define UI for dataset viewer app ---- |
| 4 |
ui <- fluidPage( |
| 5 |
|
| 6 |
# App title ---- |
| 7 |
titlePanel("Reactivity"), |
| 8 |
|
| 9 |
# Sidebar layout with input and output definitions ---- |
| 10 |
sidebarLayout( |
| 11 |
|
| 12 |
# Sidebar panel for inputs ---- |
| 13 |
sidebarPanel( |
| 14 |
|
| 15 |
# Input: Text for providing a caption ---- |
| 16 |
# Note: Changes made to the caption in the textInput control |
| 17 |
# are updated in the output area immediately as you type |
| 18 |
textInput(inputId = "caption", |
| 19 |
label = "Caption:", |
| 20 |
value = "Data Summary"), |
| 21 |
|
| 22 |
# Input: Selector for choosing dataset ---- |
| 23 |
selectInput(inputId = "dataset", |
| 24 |
label = "Choose a dataset:", |
| 25 |
choices = c("rock", "pressure", "cars")), |
| 26 |
|
| 27 |
# Input: Numeric entry for number of obs to view ---- |
| 28 |
numericInput(inputId = "obs", |
| 29 |
label = "Number of observations to view:", |
| 30 |
value = 10) |
| 31 |
|
| 32 |
), |
| 33 |
|
| 34 |
# Main panel for displaying outputs ---- |
| 35 |
mainPanel( |
| 36 |
|
| 37 |
# Output: Formatted text for caption ---- |
| 38 |
h3(textOutput("caption", container = span)), |
| 39 |
|
| 40 |
# Output: Verbatim text for data summary ---- |
| 41 |
verbatimTextOutput("summary"), |
| 42 |
|
| 43 |
# Output: HTML table with requested number of observations ---- |
| 44 |
tableOutput("view") |
| 45 |
|
| 46 |
) |
| 47 |
) |
| 48 |
) |
| 49 |
|
| 50 |
# Define server logic to summarize and view selected dataset ---- |
| 51 |
server <- function(input, output) { |
| 52 |
|
| 53 |
# Return the requested dataset ---- |
| 54 |
# By declaring datasetInput as a reactive expression we ensure |
| 55 |
# that: |
| 56 |
# |
| 57 |
# 1. It is only called when the inputs it depends on changes |
| 58 |
# 2. The computation and result are shared by all the callers, |
| 59 |
# i.e. it only executes a single time |
| 60 |
datasetInput <- reactive({ |
| 61 |
switch(input$dataset, |
| 62 |
"rock" = rock, |
| 63 |
"pressure" = pressure, |
| 64 |
"cars" = cars) |
| 65 |
}) |
| 66 |
|
| 67 |
# Create caption ---- |
| 68 |
# The output$caption is computed based on a reactive expression |
| 69 |
# that returns input$caption. When the user changes the |
| 70 |
# "caption" field: |
| 71 |
# |
| 72 |
# 1. This function is automatically called to recompute the output |
| 73 |
# 2. New caption is pushed back to the browser for re-display |
| 74 |
# |
| 75 |
# Note that because the data-oriented reactive expressions |
| 76 |
# below don't depend on input$caption, those expressions are |
| 77 |
# NOT called when input$caption changes |
| 78 |
output$caption <- renderText({ |
| 79 |
input$caption |
| 80 |
}) |
| 81 |
|
| 82 |
# Generate a summary of the dataset ---- |
| 83 |
# The output$summary depends on the datasetInput reactive |
| 84 |
# expression, so will be re-executed whenever datasetInput is |
| 85 |
# invalidated, i.e. whenever the input$dataset changes |
| 86 |
output$summary <- renderPrint({ |
| 87 |
dataset <- datasetInput() |
| 88 |
summary(dataset) |
| 89 |
}) |
| 90 |
|
| 91 |
# Show the first "n" observations ---- |
| 92 |
# The output$view depends on both the databaseInput reactive |
| 93 |
# expression and input$obs, so it will be re-executed whenever |
| 94 |
# input$dataset or input$obs is changed |
| 95 |
output$view <- renderTable({ |
| 96 |
head(datasetInput(), n = input$obs) |
| 97 |
}) |
| 98 |
|
| 99 |
} |
| 100 |
|
| 101 |
# Create Shiny app ---- |
| 102 |
shinyApp(ui, server) |