Shiny Exercise 2

Render Outputs

Published

May 5, 2025

Goal: Display Storm Name Counts in a Shiny App.

# R packages
library(shiny)
library(tidyverse)

# data preps
data(storms)

stormNames <- storms |>
  select(name, year) |>
  distinct() |>
  group_by(name) |>
  summarize(count = n()) |>
  arrange(desc(count))

# ui
ui <- fluidPage(
  
)

# server
server <- function(input, output, session) {
  
}

shinyApp(ui, server)
Click to Check the Complete Shiny App Code!
# R packages
library(shiny)
library(tidyverse)

# data preps
data(storms)

stormNames <- storms |>
  select(name, year) |>
  distinct() |>
  group_by(name) |>
  summarize(count = n()) |>
  arrange(desc(count))


# ui
ui <- fluidPage(
  "The most common storm name is", 
  
  # 1. topNameText
  textOutput("topNameText", inline = TRUE),
  
  p("The table below shows the most common storm names."),
  
  # 2. topNamesTable
  tableOutput("topNamesTable"),
  
  "The plot below shows the distribution of storm name use.",
  
  # 3. nameDist
  plotOutput("nameDist")
)

# server
server <- function(input, output, session) {
  
  # 1. topNameText
  output$topNameText <- renderText({
    slice(stormNames, 1)$name
    })
  
  # 2. topNamesTable
  output$topNamesTable <- renderTable({
    slice(stormNames, 1:5)
  })
  
  # 3. nameDist
  output$nameDist <- renderPlot({
    ggplot(data = stormNames) +
      geom_histogram(aes(x = count), binwidth = 1) +
      theme_minimal()
  })
  
}

shinyApp(ui, server)
Back to top