2025-04-10

The LM_MyApp

This Shiny application allows users to visualize and analyze birth data from the United States between the years 2000 and 2014.

It provides interactive plots to explore birth trends by year, month, and day of the week based on the US_births_2000_2014 dataset from the fivethirtyeight package.

The Shiny app and the related documentation are available at:

https://lm25.shinyapps.io/lm_myapp/

How the app looks

The application consists of two pages: one for analysis and another for documentation.

On the analysis page there is a sidebar panel and a main panel. The side panel allows the user to select a “variable” (year, month, or day of the week) to display the average number of births in the U.S. The main panel is divided into two distinct sections:

  1. Plot Section: A graphical representation of birth trends based on the selected variable

  2. Summary Section: Provides the computed year, month, or day of the week with the highest average number of births, accompanied by the corresponding value

The code for server.R is displayed in the next slide.

The code behind the app

library(shiny)
library(fivethirtyeight)
library(dplyr)
library(ggplot2)

data <- US_births_2000_2014

shinyServer(function(input, output) {
  filtered_data <- reactive({
    variable <- input$variable
    
    data %>%
      group_by_at(variable) %>%
      summarise(avg_births = mean(births)) %>%
      ungroup()
  })
  
  output$caption <- renderText({
    paste("Average Births by", input$variable)
  })
  
  output$BirthPlot <- renderPlot({
    variable <- input$variable
    filtered <- filtered_data()
    x_vals <- filtered[[variable]]
    y_vals <- filtered$avg_births
    
    ggplot(filtered, aes(x = x_vals, y = y_vals)) +
      geom_point(color = "blue", size = 3) +  
      labs(x = gsub("_", " ", variable),  
           y = "Average Number of Births") +
      theme_minimal()  
  })
  
  output$mean <- renderText({
    filtered <- filtered_data()
    max_avg <- max(filtered$avg_births)
    
    highest_value <- filtered %>%
      filter(avg_births == max_avg) %>%
      select(1) %>%
      unlist()  
    
    paste("The highest average number of births occurs in/on", 
          highest_value, "with", round(max_avg, 0), "births.")
  })
})

US average number of births per year, month and day of the week