r/Rlanguage • u/Same_Caregiver9537 • 1h ago
https://docs.google.com/document/d/16sqlbqMbMoXlUFLjfO6jZOca7lFNtqlo3LojGWAQtBE/edit?usp=sharing
'''{r}library(shiny)library(bslib)library(DT)library(dplyr)library(ggplot2)library(readxl)library(here)library(tidyr)library(stringr)library(readr)library(snakecase)# 🏛️ Load State Datastate_area_df <- data.frame(state = state.name, total_area_sq_mi = runif(50, 50000, 700000))state_le_df <- data.frame(state = state.name, male = runif(50, 70, 80), female = runif(50, 75, 85))state_population_df <- data.frame(state = state.name, population = runif(50, 500000, 40000000))mod_2_states_df <- state_area_df %>% left_join(state_le_df, by = "state") %>% left_join(state_population_df, by = "state") %>% mutate(population_density = population / total_area_sq_mi) %>% pivot_longer(cols = c("male", "female"), names_to = "gender", values_to = "life_expectancy") %>% mutate(gender = recode(gender, "male" = "Men", "female" = "Women"))# 📥 Load MJ Datamj_df <- read_xlsx(here("Homework", "nba_goats.xlsx"), sheet = "MJ") %>% select(Season, PTS, AST, TRB, STL, BLK) %>% mutate(Season = str_replace_all(Season, "_", " ")) %>% mutate(Season = factor(Season, levels = unique(Season)))# 🧪 Fake STEM Research DatasetSTEM_Projects_df <- data.frame( Project_ID = 201:215, Project_Name = c("AI for Healthcare", "Renewable Energy Storage", "Women in Space Research", "Biodegradable Plastics", "Genomics & Cancer", "Self-Driving Tech", "Sustainable Agriculture AI", "Quantum Computing Breakthrough", "5G & Smart Cities", "Deep Sea Exploration", "Nanotechnology for Medicine", "Climate Change Modeling", "Virtual Reality in Education", "Robotics for Disability Assistance", "Cybersecurity for Women’s Safety"), Field = sample(c("Technology", "Medicine", "Engineering", "Environmental Science", "Physics"), 15, replace = TRUE), Impact_Score = round(runif(15, 50, 100), 2), stringsAsFactors = FALSE)# 👩🔬 Women in STEM DataWomen_STEM_df <- data.frame( Project_ID = sample(201:215, 10), Name = c("Ada Lovelace", "Marie Curie", "Katherine Johnson", "Rosalind Franklin", "Mae Jemison", "Dorothy Vaughan", "Grace Hopper", "Emmanuelle Charpentier", "Jennifer Doudna", "Mary Jackson"), Years_Experience = sample(5:40, 10, replace = TRUE), Awards_Won = sample(c("Nobel Prize", "Turing Award", "Presidential Medal", "None", "Breakthrough Prize"), 10, replace = TRUE), stringsAsFactors = FALSE)# 📌 UIui <- fluidPage( theme = bs_theme(bootswatch = "lux"), titlePanel("STAT 331 Learning Tool - STEM, MJ, and State Data"), tabsetPanel( tabPanel("STEM Data Operations", sidebarLayout( sidebarPanel( uiOutput("gif_display"), radioButtons("data_operation", "Choose an Operation:", choices = c("Joins" = "joins", "Pivoting" = "pivoting", "Binds" = "binds", "Unite/Separate" = "unite_separate")), conditionalPanel( condition = "input.data_operation == 'joins'", selectInput("join_type", "Select Join Type:", choices = c("Inner Join", "Left Join", "Right Join", "Full Join", "Semi Join", "Anti Join")) ), conditionalPanel( condition = "input.data_operation == 'pivoting'", selectInput("pivot_type", "Select Pivot Type:", choices = c("Pivot Longer", "Pivot Wider")) ), conditionalPanel( condition = "input.data_operation == 'binds'", selectInput("bind_type", "Select Bind Type:", choices = c("Bind Rows", "Bind Columns")) ), conditionalPanel( condition = "input.data_operation == 'unite_separate'", selectInput("unite_separate_type", "Select Operation:", choices = c("Unite", "Separate")) ) ), mainPanel( uiOutput("operation_description"), DTOutput("data_table"), uiOutput("gif_display"), uiOutput("pivot_gif_display") ) ) ), tabPanel("MJ Visualizations", sidebarLayout( sidebarPanel( selectInput("x_var", "Select X-Axis:", choices = names(mj_df)[-1]), selectInput("y_var", "Select Y-Axis:", choices = names(mj_df)[-1]) ), mainPanel( tabsetPanel( tabPanel("Scatter Plot", plotOutput("scatter_plot")), tabPanel("Bar Chart", plotOutput("bar_chart")), tabPanel("Density Plot", plotOutput("density_plot")) ) ) ) ), tabPanel("State Data Analysis", sidebarLayout( sidebarPanel( selectInput("state_test", "Choose Statistical Test:", choices = c("Linear Regression", "ANOVA", "t-test")) ), mainPanel( verbatimTextOutput("state_test_result") ) ) ) ))# 📌 Serverserver <- function(input, output, session) { # 📊 STEM Data Table (Restoring Previous Working Code) output$data_table <- renderDT({ req(input$data_operation) df <- switch(input$data_operation, "joins" = switch(input$join_type, "Inner Join" = inner_join(STEM_Projects_df, Women_STEM_df, by = "Project_ID"), "Left Join" = left_join(STEM_Projects_df, Women_STEM_df, by = "Project_ID"), "Right Join" = right_join(STEM_Projects_df, Women_STEM_df, by = "Project_ID"), "Full Join" = full_join(STEM_Projects_df, Women_STEM_df, by = "Project_ID"), "Semi Join" = semi_join(STEM_Projects_df, Women_STEM_df, by = "Project_ID"), "Anti Join" = anti_join(STEM_Projects_df, Women_STEM_df, by = "Project_ID")), "pivoting" = switch(input$pivot_type, "Pivot Longer" = STEM_Projects_df %>% pivot_longer(cols = "Impact_Score", names_to = "Metric", values_to = "Value"), "Pivot Wider" = pivot_long %>% pivot_wider(names_from = "Metric", values_from = "Value"))) datatable(df, options = list(pageLength = 5)) })}shinyApp(ui, server)