library(shiny) library(ggplot2) library(dplyr) library(reshape2) setwd("/Users/janekom/Desktop/uni/THESIS/emissions_data") file_path <- "/Users/janekom/Desktop/uni/THESIS/emissions_data" # Create a function to read the files from the directory and then change the col names update_and_read_data <- function(file_path) { data <- read.csv(file_path) new_colnames <- c("type_emission", "2015", "2016", "2017", "2018", "2019", "2020", "2021", "2022") colnames(data) <- new_colnames return(data) } # Create a function to apply the "update_and_read_data" function to all files companies <- c("roche", "astra_zeneca", "bayer", "genmab", "gsk", "lonza_group", "novartis", "novo_nordisk", "novozymes", "sanofi") data_list <- list() # Loop through the companies for (company in companies) { file_path <- paste0(company, "_emissions.csv") variable_name <- paste0(company, "_data") assign(variable_name, update_and_read_data(file_path)) data_list[[variable_name]] <- get(variable_name) } # Define static colors for each emission type emission_colors <- c("deeppink2", "darkolivegreen2", "cornflowerblue", "darkmagenta", "coral", "darkgrey", "yellow", "aquamarine", "darkseagreen2", "brown1", "skyblue", "darkgreen", "deepskyblue2", "maroon", "cornsilk") # Create a mapping between emission types and colors emission_types <- unique(unlist(sapply(data_list, function(x) x$type_emission))) emission_color_mapping <- setNames(emission_colors[1:length(emission_types)], emission_types) # Ui part ui <- fluidPage( titlePanel("Footprint Assessment"), sidebarLayout( sidebarPanel(width = 3, selectInput( inputId = "company", label = "Select Company", choices = companies ), checkboxGroupInput( inputId = "emission_type", label = "Select Emission Type", choices = unique(unlist(sapply(data_list, function(x) x$type_emission)))) ), mainPanel( plotOutput("barPlot"), tableOutput("data_table") ) ) ) # Server part server <- function(input, output, session) { data <- reactive({ get(paste0(input$company, "_data")) }) observe({ updateCheckboxGroupInput(session, "emission_type", choices = unique(unlist(sapply(data_list, function(x) x$type_emission)))) }) filtered_data <- reactive({ data_subset <- data() emission_types <- input$emission_type if (length(emission_types) > 0) { data_subset <- data_subset[data_subset$type_emission %in% emission_types, ] } data_subset[, c("type_emission", "2015", "2016", "2017", "2018", "2019", "2020", "2021", "2022")] }) output$barPlot <- renderPlot({ scope_data_long <- melt(filtered_data(), id.vars = "type_emission", variable.name = "year", value.name = "emission") # Convert year to factor with original order scope_data_long$year <- factor(scope_data_long$year, levels = colnames(filtered_data())[2:ncol(filtered_data())]) # Create a bar plot with different colors for different emission types ggplot(scope_data_long, aes(x = year, y = emission, fill = type_emission)) + geom_bar(stat = "identity") + labs(title = paste("Emission Over Years for", input$company), x = "Year", y = "Emission Value", fill = "Emission Type") + scale_y_continuous(labels = scales::comma) + scale_fill_manual(values = emission_color_mapping) + # Set colors based on mapping theme_minimal() }) output$data_table <- renderTable({ filtered_data() }) } shinyApp(ui, server)