setwd("E:/Final")

# Data
sleep_data <- read.csv("Sleep and Health data set.csv", stringsAsFactors = FALSE)
colnames(sleep_data) <- gsub("\\.", " ", colnames(sleep_data))

colnames(sleep_data) <- trimws(colnames(sleep_data))

# Columns to factors
sleep_data$Occupation <- as.factor(sleep_data$Occupation)
sleep_data$`Age Group` <- as.factor(sleep_data$`Age Group`)
sleep_data$`BMI Category` <- as.factor(sleep_data$`BMI Category`)
#  BMI
bmi_colors <- c("Normal" = "lightcoral", 
                "Normal Weight" = "lightgreen", 
                "Obese" = "lightblue", 
                "Overweight" = "plum")
# User Interface

ui <- fluidPage(
  titlePanel("Sleep and Health Dashboard"),
  sidebarLayout(
    sidebarPanel(
      selectInput("ageGroupFilter", "Select Age Group:", choices = levels(sleep_data$`Age Group`), selected = NULL, multiple = TRUE),
      selectInput("occupationFilter", "Select Occupation:", choices = levels(sleep_data$Occupation), selected = NULL, multiple = TRUE),
      width = 3
    ),
    mainPanel(
      tabsetPanel(
        tabPanel("Panel 1: Age & Stress vs Quality of Sleep", plotOutput("stressQualityPlot")),
        tabPanel("Panel 2: Occupation vs Sleep Duration", plotlyOutput("occupationSleepPlot")),
        tabPanel("Panel 3: Physical Activity vs Quality of Sleep", plotlyOutput("physicalActivityPlot"))
      ),
      width = 9
    )
  )
)
# Web

server <- function(input, output, session) {
  observeEvent(input$ageGroupFilter, {
    available_occupations <- sleep_data %>% 
      filter(`Age Group` %in% input$ageGroupFilter) %>% 
      pull(Occupation) %>% 
      unique()
    
    updateSelectInput(session, "occupationFilter", choices = available_occupations)
  })
  
  # Reactive data filtering based on inputs
  filtered_data <- reactive({
    data <- sleep_data
    if (!is.null(input$ageGroupFilter) && length(input$ageGroupFilter) > 0) {
      data <- data %>% filter(`Age Group` %in% input$ageGroupFilter)
    }
    if (!is.null(input$occupationFilter) && length(input$occupationFilter) > 0) {
      data <- data %>% filter(Occupation %in% input$occupationFilter)
    }
    return(data)
  })
  
  # Visual 1: Age & Stress vs Quality of Sleep
  output$stressQualityPlot <- renderPlot({
    ggplot(filtered_data(), aes(x = Age, y = `Quality of Sleep`, size = `Stress Level`, color = `Stress Level`)) +
      geom_point(alpha = 0.6) +
      scale_size(range = c(3, 10)) +
      theme_minimal() +
      labs(title = "Age and Stress Level vs Quality of Sleep",
           x = "Age",
           y = "Quality of Sleep",
           size = "Stress Level") +
      scale_color_gradient(low = "lightblue", high = "darkblue")
  })
  
  # Visual 2: Occupation vs Sleep Duration
  output$occupationSleepPlot <- renderPlotly({
    selected_ages <- paste(input$ageGroupFilter, collapse = ", ")
    plot_title <- if (selected_ages == "") "Sleep Duration by Occupation" else paste("Sleep Duration by Occupation for Age Groups:", selected_ages)
    
    plot <- ggplot(filtered_data(), aes(x = Occupation, y = `Sleep Duration`)) +
      geom_boxplot(fill = "lightgreen") +
      labs(title = plot_title, y = "Sleep Duration (hours)") +
      theme_minimal() +
      theme(axis.text.x = element_text(angle = 45, hjust = 1))
    
    ggplotly(plot)
  })
  
  # Visual 3: Physical Activity vs Quality of Sleep
  output$physicalActivityPlot <- renderPlotly({
    plot <- ggplot(filtered_data(), aes(x = `Physical Activity Level`, y = `Quality of Sleep`, color = `BMI Category`)) +
      geom_point(alpha = 0.6, size = 3) +
      scale_color_manual(values = bmi_colors) +
      labs(title = "Physical Activity Level vs Quality of Sleep", x = "Physical Activity Level", y = "Quality of Sleep", color = "BMI Category") +
      theme_minimal()
    
    ggplotly(plot)
  })
}
shinyApp(ui = ui, server = server)
Shiny applications not supported in static R Markdown documents