Introduction
R is a powerful programming language for statistical computing and data visualization. When combined with the vast datasets available from the World Bank and the interactivity of Streamlit, we can create insightful and interactive data dashboards with minimal effort. In this blog, we will walk through the process of using R to analyze World Bank data and then display the results in a Streamlit dashboard.
Why Use R for World Bank Data Analysis?
- Data Handling: R provides numerous packages (e.g.,
tidyverse
,dplyr
) that make data cleaning and manipulation easier. - Statistical Analysis: R is built for statistical computing, making it an ideal choice for analyzing socio-economic and financial data.
- Data Visualization: Packages like
ggplot2
andplotly
help create professional-quality visualizations. - APIs and Data Integration: The
WDI
package in R allows seamless access to World Bank datasets.
Getting Started
1. Install Necessary Packages
To begin, install the required R packages by running:
install.packages("tidyverse")
install.packages("WDI")
install.packages("plotly")
install.packages("shiny")
2. Fetch World Bank Data
Using the WDI
package, we can easily fetch global economic data. Below is an example of retrieving GDP data for multiple countries:
library(WDI)
library(tidyverse)
data <- WDI(indicator = "NY.GDP.MKTP.CD", country = c("US", "IN", "CN", "BR"), start = 2000, end = 2023)
head(data)
3. Data Cleaning and Transformation
We often need to clean and reshape the data before visualization:
data_clean <- data %>%
rename(GDP = NY.GDP.MKTP.CD) %>%
mutate(year = as.factor(year))
4. Create Visualizations
Using ggplot2
, we can generate interactive visualizations:
library(ggplot2)
ggplot(data_clean, aes(x = year, y = GDP, color = country)) +
geom_line() +
theme_minimal() +
labs(title = "GDP Growth Over Time", x = "Year", y = "GDP (USD)")
Deploying with Streamlit
While R has Shiny
for web applications, we can also use Python’s Streamlit
for an interactive frontend. First, install Streamlit and Pandas in Python:
pip install streamlit pandas
1. Convert R Output to CSV
Save your processed R data as a CSV file:
write.csv(data_clean, "worldbank_gdp.csv", row.names = FALSE)
2. Build a Streamlit App
Create a app.py
file and add the following Python code:
import streamlit as st
import pandas as pd
import plotly.express as px
st.title("World Bank GDP Analysis")
data = pd.read_csv("worldbank_gdp.csv")
fig = px.line(data, x="year", y="GDP", color="country", title="GDP Growth Over Time")
st.plotly_chart(fig)
3. Run the Streamlit App
Execute the following command in the terminal:
streamlit run app.py
Conclusion
By combining R’s powerful statistical capabilities with Streamlit’s interactive frontend, we can create a dynamic and insightful dashboard using World Bank data. This setup is ideal for economists, data analysts, and researchers looking to explore large datasets in an accessible way. Try it out and enhance your data storytelling capabilities!
Would you like to explore additional datasets or advanced visualizations? Let us know in the comments!
Leave a Reply