Building an Environmental Metrics Dashboard in R Using Shiny

 

In today's data-driven world, tracking environmental indicators such as CO2 emissions, renewable energy consumption, and energy use is essential for understanding and addressing global challenges. This blog walks you through building an Environmental Metrics Dashboard in R using the Shiny framework, enabling users to visualize and compare these critical indicators across multiple countries over time.

Why Build an Environmental Metrics Dashboard?

Environmental data, such as CO2 emissions per capita and renewable energy consumption, provide invaluable insights into a country's progress toward sustainability. By visualizing these metrics interactively, stakeholders and decision-makers can identify trends, compare countries, and focus on actionable solutions.

The dashboard we’ll create allows users to:

  • Compare multiple countries across key environmental metrics.
  • Explore trends over time.
  • Customize visualizations with a user-friendly interface.

Key Components of the Dashboard

Our dashboard uses the World Bank's API to fetch environmental data and includes three key metrics:

  1. CO2 Emissions (Metric Tons per Capita): Represents the average carbon dioxide emissions per person.
  2. Renewable Energy Consumption (% of Total): Measures the proportion of energy derived from renewable sources.
  3. Energy Use (kg of Oil Equivalent per Capita): Indicates the average energy consumption per person.

Step-by-Step Implementation

1. Fetching the Data

We use the WDI package to pull environmental data from the World Bank's API for selected countries between 2000 and 2024. The data is preprocessed for easier analysis and visualization.


library(WDI) library(dplyr) # Define Environmental Indicators indicators <- c( "EN.ATM.CO2E.PC", # CO2 emissions (metric tons per capita) "EG.FEC.RNEW.ZS", # Renewable energy consumption (% of total final energy consumption) "EG.USE.PCAP.KG.OE" # Energy use (kg of oil equivalent per capita) ) # Fetch Data from World Bank API wdi_data <- WDI( country = "all", indicator = indicators, start = 2000, end = 2024, extra = TRUE ) # Rename Columns for Readability colnames(wdi_data)[which(colnames(wdi_data) %in% indicators)] <- c( "CO2 Emissions (Metric Tons per Capita)", "Renewable Energy Consumption (% of Total)", "Energy Use (kg of Oil Equivalent per Capita)" ) # Filter for Selected Countries countries_to_compare <- c( "Australia", "Canada", "China", "Denmark", "Germany", "India", "Japan", "Netherlands", "Norway", "United States" ) comparison_data <- subset(wdi_data, country %in% countries_to_compare) # Clean Dataset comparison_data <- comparison_data %>% select(-c(iso2c, status, lastupdated, capital, lending)) %>% distinct()

2. Building the User Interface

The Shiny app's UI provides:

  • Dropdown menus to select countries and metrics.
  • A slider to filter data by year.
  • A dynamic plot for visualizing the selected metrics.

library(shiny) library(shinydashboard) ui <- dashboardPage( dashboardHeader(title = "Environmental Metrics Dashboard"), dashboardSidebar( sidebarMenu( menuItem("Comparison Dashboard", tabName = "dashboard", icon = icon("dashboard")), selectInput( "country", "Select Countries:", choices = unique(comparison_data$country), selected = unique(comparison_data$country)[1:3], multiple = TRUE ), selectInput( "metric", "Select Metric:", choices = colnames(comparison_data)[4:6], # Metrics columns selected = colnames(comparison_data)[4], multiple = FALSE ), sliderInput( "year", "Select Year:", min = min(comparison_data$year, na.rm = TRUE), max = max(comparison_data$year, na.rm = TRUE), value = c(min(comparison_data$year, na.rm = TRUE), max(comparison_data$year, na.rm = TRUE)), step = 1, sep = "" ) ) ), dashboardBody( tabItems( tabItem( tabName = "dashboard", fluidRow( box( title = "Environmental Metrics by Year", status = "primary", solidHeader = TRUE, width = 12, plotOutput("metricPlot", height = "500px") ) ) ) ) ) )

3. Adding the Server Logic

The server logic processes user inputs and dynamically updates the plot. The key tasks include filtering the dataset and generating the visualization.


server <- function(input, output) { # Filter Data Based on Inputs filtered_data <- reactive({ comparison_data %>% filter( country %in% input$country, year >= input$year[1] & year <= input$year[2] ) }) # Generate Plot output$metricPlot <- renderPlot({ data <- filtered_data() ggplot(data, aes(x = year, y = .data[[input$metric]], color = country)) + geom_line(size = 1) + geom_point(size = 2) + labs( title = paste("Comparison of", input$metric, "by Year"), x = "Year", y = input$metric ) + theme_minimal() + theme( legend.position = "bottom", legend.title = element_blank(), text = element_text(size = 12) ) }) }

4. Running the Dashboard

Finally, combine the UI and server components to launch the app.


# Run the App shinyApp(ui, server)

Features of the Dashboard

  • Interactive Visualization: Select countries, metrics, and years for a tailored view of the data.
  • Comparison Across Countries: Analyze trends across multiple nations simultaneously.
  • Customizable Metrics: Switch between key indicators like CO2 emissions and renewable energy consumption.

Insights and Use Cases

This dashboard can be used by:

  • Policymakers to evaluate and compare environmental progress.
  • Researchers to analyze trends and correlations in energy and emissions data.
  • Educators to demonstrate real-world applications of environmental data.


Comments

Popular posts from this blog

Medical Data Set Clustering Analysis Using R-Programming