Spring 2026

MATH 261: Intro to Data Science

Assignment #6 | Total points: 10

** Emily Fitch **

Date: Wednesday, 4/22/2026


Important Instructions:




Question.
[10 pts.]


You goal is to create an interactive web application using the Shiny package in R to analyze student activity patterns on campus. Each of you will work with a unique dataset generated from your own ID number.


Data Generation

Use the following code to generate your dataset. You must use the last three digits of your student ID as the seed value.


set.seed(200)  # replace XXX with last three digits of your student ID

n <- 60
location <- sample(c("Library", "Gym", "Cafeteria", "Dorm"), n, replace = TRUE)
hours <- round(runif(n, 0, 5), 1)
day <- sample(c("Weekday", "Weekend"), n, replace = TRUE)

df <- data.frame(location, hours, day)


Create a Shiny application with the following requirements:


Your app must include a title panel displaying the name of your application and a sidebar layout. Inside the sidebar panel, include:
- A dropdown menu to select a campus location
- A slider to select a range of hours spent on campus
- A checkbox to show or hide the plot
- A checkbox to show or hide the average hours value

The main panel of your app must include:
- A histogram showing the distribution of hours spent on campus
- A text output showing the average number of hours spent

Your app must:
- Filter the dataset based on user-selected location and hours range
- Dynamically update the histogram when inputs change
- Dynamically update the mean value
- Respect checkbox selections (show/hide outputs)


Your app will be similar to the one shown below.


library(shiny)
## Warning: package 'shiny' was built under R version 4.5.3
library(ggplot2)

set.seed(200)

n <- 60
location <- sample(c("Library", "Gym", "Cafeteria", "Dorm"), n, replace = TRUE)
hours <- round(runif(n, 0, 5), 1)
day <- sample(c("Weekday", "Weekend"), n, replace = TRUE)

df <- data.frame(location, hours, day)

ui <- fluidPage(
  titlePanel("Campus Activity Patterns Analyzer"),
  
  sidebarLayout(
    sidebarPanel(
      selectInput("location", "Select Campus Location:",
                  choices = unique(df$location),
                  selected = unique(df$location)[1]),
      
      sliderInput("hoursRange", "Select Hours Spent on Campus:",
                  min = 0, max = 5, value = c(0, 5)),
      
      checkboxInput("showPlot", "Show Histogram", value = TRUE),
      checkboxInput("showMean", "Show Average Hours", value = TRUE)
    ),
    
    mainPanel(
      conditionalPanel(
        condition = "input.showPlot == true",
        plotOutput("histPlot")
      ),
      
      conditionalPanel(
        condition = "input.showMean == true",
        textOutput("meanText")
      )
    )
  )
)

server <- function(input, output) {
  
  filtered_data <- reactive({
    df[
      df$location == input$location &
        df$hours >= input$hoursRange[1] &
        df$hours <= input$hoursRange[2],
    ]
  })
  
  output$histPlot <- renderPlot({
    data <- filtered_data()
    
    validate(
      need(nrow(data) > 0, "No data")
    )
    
    ggplot(data, aes(x = hours)) +
      geom_histogram(
        bins = 7,
        fill = "pink",
        color = "white",
        alpha = 0.9
      ) +
      labs(
        title = paste("Distribution of Hours at", input$location),
        x = "Hours Spent",
        y = "Count"
      ) +
      theme_minimal(base_size = 14) +
      theme(
        plot.title = element_text(face = "bold", hjust = 0.5),
        axis.title.y = element_text(angle = 0, vjust = 0.5),
        panel.grid.minor = element_blank()
      )
  })
  
  output$meanText <- renderText({
    data <- filtered_data()
    
    if (nrow(data) == 0) {
      return("No data available for selected filters.")
    }
  
    paste("Average hours spent:", round(mean(data$hours), 2))
  })
}
shinyApp(ui = ui, server = server)
Shiny applications not supported in static R Markdown documents

App