Lecture 20
            Cornell University 
 INFO 5001 - Fall 2025
          
November 6, 2025
TODO
TODO add this script
# get county names
county_names <- climate_sf |>
  arrange(STATEFP) |>
  pull(county)
# extract county/state labels as character vector
selectizeInput(
  inputId = "county",
  label = "Selected county",
  choices = county_names,
  selected = NULL,
  # custom selectize.js options
  options = list(
    # placeholder text
    placeholder = "Select a county",
    # limit to one county at a time
    maxItems = 1
  )
)# define hazard types
hazard_types <- climate_risk |>
  select(contains("hazard")) |>
  colnames()
# create human-readable labels
hazard_types_labels <- hazard_types |>
  str_remove(pattern = "_hazard_type_risk_index_score") |>
  make_clean_names(case = "title")
# create a named character vector for the input
names(hazard_types) <- hazard_types_labels
# identify columns with hazard risks and extract column names
checkboxGroupInput(
  inputId = "hazard_types",
  label = "Hazard types",
  # all possible choices
  choices = hazard_types,
  # initialize plot with all individual hazards
  selected = hazard_types
)output$render*()#| '!! shinylive warning !!': |
#|   shinylive does not work in self-contained HTML documents.
#|   Please set `embed-resources: false` in your metadata.
#| standalone: true
#| viewerHeight: 700
library(shiny)
library(bslib)
library(ggplot2)
library(palmerpenguins)
# Define your Shiny UI here
ui <- page_sidebar(
  sidebar = sidebar(
    sliderInput("bins", "Number of bins:",
      min = 1, max = 50, value = 30
    )
  ),
  plotOutput("distPlot")
)
# Define your Shiny server logic here
server <- function(input, output) {
  # Your server code goes here
  output$distPlot <- renderPlot({
    ggplot(data = penguins, mapping = aes(x = body_mass_g)) +
      geom_histogram(bins = input$bins)
  })
}
# Create and launch the Shiny app
shinyApp(ui, server)*Output() \(\rightarrow\) render*()| Output function | Render function | 
|---|---|
| plotOutput() | renderPlot({}) | 
| tableOutput() | renderTable({}) | 
| uiOutput() | renderUI({}) | 
| textOutput() | renderText({}) | 
render*() functionsoutput$render*()input$x changes, anything that relies on x is re-evaluatedinput$bins is a reactive valueoutput$distPlot depends on input$bins
input$bins changes \(\rightarrow\) output$distPlot reactsrender* function, the output will re-render any time input changesrender* function is a reactive contextreactive({...}) to assign a reactive variableobserve({...}) to access a reactive variablelibrary(shiny)
library(dplyr)
library(ggplot2)
library(bslib)
ui <- page_sidebar(
  title = "Simulate a normal distribution",
  sidebar = sidebar(
    sliderInput(
      inputId = "num",
      label = "Choose a sample size",
      min = 0,
      max = 1000,
      value = 200
    )
  ),
  card(
    card_header("Plot"),
    plotOutput(outputId = "myplot")
  )
)
server <- function(input, output, session) {
  set.seed(123)
  # simulate the data
  df <- reactive({
    tibble(
      x = rnorm(n = input$num)
    )
  })
  # visualize the distribution
  output$myplot <- renderPlot({
    ggplot(data = df(), mapping = aes(x = x)) +
      geom_histogram() +
      theme_bw(base_size = 14)
  })
  # print a message to the console logs
  observe({
    message(str_glue("Current number of observations: {input$num}"))
  })
}
shinyApp(ui, server)uiOutput()library(shiny)
library(bslib)
ui <- page_fluid(
  checkboxInput("show_slider", label = "Show slider", value = TRUE),
  uiOutput("slider_ui")
)
server <- function(input, output) {
  output$slider_ui <- renderUI({
    if (input$show_slider) {
      sliderInput("slider", "Slider", 1, 10, 5)
    }
  })
}
shinyApp(ui = ui, server = server)#| '!! shinylive warning !!': |
#|   shinylive does not work in self-contained HTML documents.
#|   Please set `embed-resources: false` in your metadata.
#| label: checkbox-slider
#| standalone: true
#| viewerHeight: 475
library(shiny)
library(bslib)
ui <- page_fluid(
  checkboxInput("show_slider", label = "Show slider", value = TRUE),
  uiOutput("slider_ui") 
)
server <- function(input, output) {
  output$slider_ui <- renderUI({ 
    if (input$show_slider) { 
      sliderInput("slider", "Slider", 1, 10, 5) 
    } 
  }) 
}
shinyApp(ui = ui, server = server)ae-18Instructions
ae-18 (repo name will be suffixed with your GitHub name).renv::restore() to install the required packages, open the Quarto document in the repo, and follow along and complete the exercises.Instructions
Implement server logic for reactive content
output list object