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