Using wdi2 to access World Development Indicators

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.

Installation

You can install the released version of wdi2 from CRAN via:

install.packages("wdi2")

To install the development version of wdi2, use:

pak::pak("tidy-intelligence/r-wdi2")

Listing supported indicators

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()
#> # A tibble: 48,918 × 4
#>    indicator_id         indicator_name           source_note source_organization
#>    <chr>                <chr>                    <chr>       <chr>              
#>  1 1.0.HCount.1.90usd   Poverty Headcount ($1.9… The povert… LAC Equity Lab tab…
#>  2 1.0.HCount.1.90usd   Poverty Headcount ($1.9… The povert… LAC Equity Lab tab…
#>  3 1.0.HCount.2.5usd    Poverty Headcount ($2.5… The povert… LAC Equity Lab tab…
#>  4 1.0.HCount.2.5usd    Poverty Headcount ($2.5… The povert… LAC Equity Lab tab…
#>  5 1.0.HCount.Mid10to50 Middle Class ($10-50 a … The povert… LAC Equity Lab tab…
#>  6 1.0.HCount.Mid10to50 Middle Class ($10-50 a … The povert… LAC Equity Lab tab…
#>  7 1.0.HCount.Ofcl      Official Moderate Pover… The povert… LAC Equity Lab tab…
#>  8 1.0.HCount.Ofcl      Official Moderate Pover… The povert… LAC Equity Lab tab…
#>  9 1.0.HCount.Poor4uds  Poverty Headcount ($4 a… The povert… LAC Equity Lab tab…
#> 10 1.0.HCount.Poor4uds  Poverty Headcount ($4 a… The povert… LAC Equity Lab tab…
#> # ℹ 48,908 more rows

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)
#> Parsing indicators: ■■■■                              11% |  ETA: 24s
#> Parsing indicators: ■■■■■■■■                          23% |  ETA: 20s
#> Parsing indicators: ■■■■■■■■■■■                       35% |  ETA: 17s
#> Parsing indicators: ■■■■■■■■■■■■■■■                   48% |  ETA: 13s
#> Parsing indicators: ■■■■■■■■■■■■■■■■■■                58% |  ETA: 11s
#> Parsing indicators: ■■■■■■■■■■■■■■■■■■■■■             65% |  ETA:  9s
#> Parsing indicators: ■■■■■■■■■■■■■■■■■■■■■■■■■         79% |  ETA:  6s
#> Parsing indicators: ■■■■■■■■■■■■■■■■■■■■■■■■■■■       87% |  ETA:  3s
#> Parsing indicators: ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■    97% |  ETA:  1s
#> # A tibble: 24,459 × 8
#>    indicator_id         indicator_name  unit  source_id source_value source_note
#>    <chr>                <chr>           <chr> <chr>     <chr>        <chr>      
#>  1 1.0.HCount.1.90usd   Poverty Headco… <NA>  37        LAC Equity … "The pover…
#>  2 1.0.HCount.2.5usd    Poverty Headco… <NA>  37        LAC Equity … "The pover…
#>  3 1.0.HCount.Mid10to50 Middle Class (… <NA>  37        LAC Equity … "The pover…
#>  4 1.0.HCount.Ofcl      Official Moder… <NA>  37        LAC Equity … "The pover…
#>  5 1.0.HCount.Poor4uds  Poverty Headco… <NA>  37        LAC Equity … "The pover…
#>  6 1.0.HCount.Vul4to10  Vulnerable ($4… <NA>  37        LAC Equity … "The pover…
#>  7 1.0.PGap.1.90usd     Poverty Gap ($… <NA>  37        LAC Equity … "The pover…
#>  8 1.0.PGap.2.5usd      Poverty Gap ($… <NA>  37        LAC Equity … "The pover…
#>  9 1.0.PGap.Poor4uds    Poverty Gap ($… <NA>  37        LAC Equity … "The pover…
#> 10 1.0.PSev.1.90usd     Poverty Severi… <NA>  37        LAC Equity … "The pover…
#> # ℹ 24,449 more rows
#> # ℹ 2 more variables: source_organization <chr>, topics <list>

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)
#> # A tibble: 24,459 × 8
#>    indicator_id         indicator_name  unit  source_id source_value source_note
#>    <chr>                <chr>           <chr> <chr>     <chr>        <chr>      
#>  1 1.0.HCount.1.90usd   "Tasa de Incid… <NA>  37        <NA>         "Tasa de I…
#>  2 1.0.HCount.2.5usd    "Tasa de Incid… <NA>  37        <NA>         "Tasa de I…
#>  3 1.0.HCount.Mid10to50 "Tasa de Incid… <NA>  37        <NA>         "Tasa de I…
#>  4 1.0.HCount.Ofcl      "Tasa Oficial … <NA>  37        <NA>         "Tasa de I…
#>  5 1.0.HCount.Poor4uds  "Tasa de Incid… <NA>  37        <NA>         "Tasa de I…
#>  6 1.0.HCount.Vul4to10  "Tasa de incid… <NA>  37        <NA>         "Tasa de I…
#>  7 1.0.PGap.1.90usd     "Brecha de Pob… <NA>  37        <NA>         "La Brecha…
#>  8 1.0.PGap.2.5usd      "Brecha de Pob… <NA>  37        <NA>         "La Brecha…
#>  9 1.0.PGap.Poor4uds    "Brecha de Pob… <NA>  37        <NA>         "La Brecha…
#> 10 1.0.PSev.1.90usd     "Severidad de … <NA>  37        <NA>         "El índice…
#> # ℹ 24,449 more rows
#> # ℹ 2 more variables: source_organization <chr>, topics <list>

To get the list of supported languages, call:

# List supported languages
list_supported_languages()
#> # A tibble: 5 × 2
#>   code  name   
#>   <chr> <chr>  
#> 1 en    English
#> 2 es    Spanish
#> 3 fr    French 
#> 4 ar    Arabic 
#> 5 zh    Chinese

Note that not all indicators might be translated into all languages.

Listing supported Countries

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()
#> # A tibble: 296 × 10
#>    id    iso2_code name   capital_city longitude latitude regions  admin_regions
#>    <chr> <chr>     <chr>  <chr>        <chr>     <chr>    <list>   <list>       
#>  1 ABW   AW        Aruba  Oranjestad   -70.0167  12.5167  <tibble> <tibble>     
#>  2 AFE   ZH        Afric… <NA>         <NA>      <NA>     <tibble> <tibble>     
#>  3 AFG   AF        Afgha… Kabul        69.1761   34.5228  <tibble> <tibble>     
#>  4 AFR   A9        Africa <NA>         <NA>      <NA>     <tibble> <tibble>     
#>  5 AFW   ZI        Afric… <NA>         <NA>      <NA>     <tibble> <tibble>     
#>  6 AGO   AO        Angola Luanda       13.242    -8.81155 <tibble> <tibble>     
#>  7 ALB   AL        Alban… Tirane       19.8172   41.3317  <tibble> <tibble>     
#>  8 AND   AD        Andor… Andorra la … 1.5218    42.5075  <tibble> <tibble>     
#>  9 ARB   1A        Arab … <NA>         <NA>      <NA>     <tibble> <tibble>     
#> 10 ARE   AE        Unite… Abu Dhabi    54.3705   24.4764  <tibble> <tibble>     
#> # ℹ 286 more rows
#> # ℹ 2 more variables: income_levels <list>, lending_types <list>

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")

Downloading indicator data

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"))
#> # A tibble: 384 × 4
#>    indicator_id   country_id date   value
#>    <chr>          <chr>      <chr>  <dbl>
#>  1 NY.GDP.PCAP.KD CA         2023  44388.
#>  2 NY.GDP.PCAP.KD CA         2022  45227.
#>  3 NY.GDP.PCAP.KD CA         2021  44360.
#>  4 NY.GDP.PCAP.KD CA         2020  42366.
#>  5 NY.GDP.PCAP.KD CA         2019  45100.
#>  6 NY.GDP.PCAP.KD CA         2018  44907.
#>  7 NY.GDP.PCAP.KD CA         2017  44339.
#>  8 NY.GDP.PCAP.KD CA         2016  43551.
#>  9 NY.GDP.PCAP.KD CA         2015  43594.
#> 10 NY.GDP.PCAP.KD CA         2014  43643.
#> # ℹ 374 more rows

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"))

Conclusion

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.