knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(wdi2)
The wdi2
package provides a modern and flexible interface for accessing the World Bank’s World Development Indicators (WDI). It improves upon existing packages by utilizing the httr2
package for multi-page requests and enhanced error handling. With wdi2
, you can download data for multiple indicators and countries in a single function call, benefit from progress bars, and receive the output in a tidy data format, making it ideal for further analysis.
You can install the released version of wdi2
from CRAN via:
#| eval: false install.packages("wdi2")
To install the development version of wdi2
, use:
#| eval: false pak::pak("tidy-intelligence/r-wdi2")
The wdi2
package allows you to retrieve a full list of all supported indicators from the World Bank Indicators API. Each indicator is accompanied by metadata such as its unit of measurement, source, and associated topics.
# Retrieve the list of supported indicators list_supported_indicators()
If you want to get more details such as source ID and topics, you can use the corresponding option:
# Retrieve the list of supported indicators with details list_supported_indicators(include_details = TRUE)
You can also get the list of supported indicators in a different language, or omit the progress bar. For example, to retrieve the indicators in Spanish:
# Retrieve indicators in Spanish list_supported_indicators(language = "es", include_details = TRUE, progress = FALSE)
To get the list of supported languages, call:
# List supported languages list_supported_languages()
Note that not all indicators might be translated into all languages.
You can retrieve a list of all countries supported by the World Bank API along with metadata such as region, administrative region, income level, and lending type.
# Retrieve the list of supported countries list_supported_countries()
This information can also be requested in other languages. For example, to view the supported countries in Chinese:
# Retrieve countries in Chinese list_supported_countries(language = "zh")
With wdi2
, you can download indicator data for multiple countries and indicators in a single function call. The function returns a tidy data frame with country, indicator, date, and value columns.
For example, to download GDP per capita and total population for Mexico, Canada, and the United States:
# Download specific indicators for selected countries download_indicators(countries = c("MX", "CA", "US"), indicators = c("NY.GDP.PCAP.KD", "SP.POP.TOTL"))
If you need the same indicators for all countries, you can pass "all"
as the countries
parameter:
# Download indicators for all countries download_indicators(countries = "all", indicators = c("NY.GDP.PCAP.KD", "SP.POP.TOTL"))
The wdi2
package is designed to simplify the process of accessing and analyzing World Bank data. By offering features like multi-indicator downloads, progress bars, and flexible language support, wdi2
is a robust tool for users who need access to World Development Indicators in a modern, tidy format.
If you encounter any errors or have suggestions for improvements, please consider opening an issue in the package repository on GitHub.
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.