| Type: | Package |
| Title: | Programmatic Access to Data and Statistics from the World Bank API |
| Version: | 1.2 |
| Description: | Search and download data from the World Bank Data API. Includes support for mutliple languages, access to annual, quarterly, and monthly data. |
| License: | Apache License 2.0 |
| URL: | https://github.com/pachadotdev/wbstats |
| BugReports: | https://github.com/pachadotdev/wbstats/issues |
| Depends: | R (≥ 3.5) |
| Imports: | data.table, httr, jsonlite |
| Suggests: | litedown, tinytest, vcr, |
| Encoding: | UTF-8 |
| LazyData: | TRUE |
| VignetteBuilder: | litedown |
| NeedsCompilation: | no |
| Packaged: | 2026-08-26 12:36:26 UTC; pacha |
| Author: | Mauricio Vargas Sepulveda
|
| Maintainer: | Mauricio Vargas Sepulveda <m.vargas.sepulveda@gmail.com> |
| Repository: | CRAN |
| Date/Publication: | 2026-08-28 09:50:02 UTC |
Download an updated list of country, indicator, and source information
Description
Download an updated list of information regarding countries, indicators, sources, regions, indicator topics, lending types, income levels, and supported languages from the World Bank API
Usage
wb_cache(lang)
Arguments
lang |
Language in which to return the results. If |
Value
A list containing the following items:
-
countries: The result of callingwb_countries() -
indicators: The result of callingwb_indicators() -
sources: The result of callingwb_sources() -
topics: The result of callingwb_topics() -
regions: The result of callingwb_regions() -
income_levels: The result of callingwb_income_levels() -
lending_types: The result of callingwb_lending_types() -
languages: The result of callingwb_languages()
Cached information from the World Bank API
Description
This data is a cached result of the wb_cache function.
By default functions wb_data and wb_search use this
data for the cache parameter.
Summarize Data Coverage for World Bank Indicators
Description
Given a indicator available from the World Bank API, this functions returns a pre-computed summary stored on GitHub and not available from the API that shows the percentage of non-missing (non-NA) observations for every country, the year range per country, and the total number of observations per country. This is useful before downloading a dataset.
Usage
wb_country_coverage(pattern, country, start_date, end_date, cache)
Arguments
pattern |
A character string or regular expression (e.g., |
country |
Character vector of country, region, or special value codes for the locations you want to return data for. Permissible values can be found in the countries data frame in wb_cachelist or by running
|
start_date |
Numeric or character. If numeric it must be in |
end_date |
Numeric or character. If numeric it must be in |
cache |
List of data frames returned from |
Value
A data.table with one row per country.
Examples
## Not run:
wb_country_coverage("gross domestic product", c("Mexico", "Chile"), 2010, 2020)
## End(Not run)
Download Data from the World Bank API
Description
This function downloads the requested information using the World Bank API
Usage
wb_data(
indicator,
country = "countries_only",
start_date,
end_date,
return_wide = TRUE,
mrv,
mrnev,
cache,
freq,
gapfill = FALSE,
date_as_class_date = FALSE,
lang
)
Arguments
indicator |
Character vector of indicator codes. These codes correspond to the |
country |
Character vector of country, region, or special value codes for the locations you want to return data for. Permissible values can be found in the countries data frame in wb_cachelist or by running
|
start_date |
Numeric or character. If numeric it must be in |
end_date |
Numeric or character. If numeric it must be in |
return_wide |
Logical. If |
mrv |
Numeric. The number of Most Recent Values to return. A replacement of |
mrnev |
Numeric. The number of Most Recent Non Empty Values to return. A replacement of |
cache |
List of data frames returned from |
freq |
Character String. For fetching quarterly ("Q"), monthly("M") or yearly ("Y") values. Useful for querying high frequency data. |
gapfill |
Logical. If |
date_as_class_date |
Logical. If |
lang |
Language in which to return the results. If |
Details
obs_status column
Indicates the observation status for location, indicator and date combination. For example "F" in the response indicates that the observation status for that data point is "forecast".
Value
a data.table/data.frame of all available requested data.
Examples
# NOTE: These examples are wrapped in a "don't run" because these
# require an internet connection
# gdp for all countries for all available dates
## Not run:
df_gdp <- wb_data("NY.GDP.MKTP.CD")
## End(Not run)
# Brazilian gdp for all available dates
## Not run:
df_brazil <- wb_data("NY.GDP.MKTP.CD", country = "br")
## End(Not run)
# Brazilian gdp for 2006
## Not run:
df_brazil_1 <- wb_data("NY.GDP.MKTP.CD", country = "brazil", start_date = 2006)
## End(Not run)
# Brazilian gdp for 2006-2010
## Not run:
df_brazil_2 <- wb_data("NY.GDP.MKTP.CD",
country = "BRA",
start_date = 2006, end_date = 2010
)
## End(Not run)
# Population, GDP, Unemployment Rate, Birth Rate (per 1000 people)
## Not run:
my_indicators <- c(
"SP.POP.TOTL",
"NY.GDP.MKTP.CD",
"SL.UEM.TOTL.ZS",
"SP.DYN.CBRT.IN"
)
## End(Not run)
## Not run:
df <- wb_data(my_indicators)
## End(Not run)
# you pass multiple country ids of different types
# Albania (iso2c), Georgia (iso3c), and Mongolia
## Not run:
my_countries <- c("AL", "Geo", "mongolia")
df <- wb_data(my_indicators,
country = my_countries,
start_date = 2005, end_date = 2007
)
## End(Not run)
# same data as above, but in long format
## Not run:
df_long <- wb_data(my_indicators,
country = my_countries,
start_date = 2005, end_date = 2007,
return_wide = FALSE
)
## End(Not run)
# regional population totals
# regions correspond to the region column in wb_cachelist$countries
## Not run:
df_region <- wb_data("SP.POP.TOTL",
country = "regions_only",
start_date = 2010, end_date = 2014
)
## End(Not run)
# a specific region
## Not run:
df_world <- wb_data("SP.POP.TOTL",
country = "world",
start_date = 2010, end_date = 2014
)
## End(Not run)
# if the indicator is part of a named vector the name will be the column name
my_indicators <- c(
"pop" = "SP.POP.TOTL",
"gdp" = "NY.GDP.MKTP.CD",
"unemployment_rate" = "SL.UEM.TOTL.ZS",
"birth_rate" = "SP.DYN.CBRT.IN"
)
## Not run:
df_names <- wb_data(my_indicators,
country = "world",
start_date = 2010, end_date = 2014
)
## End(Not run)
# custom names are ignored if returning in long format
## Not run:
df_names_long <- wb_data(my_indicators,
country = "world",
start_date = 2010, end_date = 2014,
return_wide = FALSE
)
## End(Not run)
# same as above but in Bulgarian
# note that not all indicators have translations for all languages
## Not run:
df_names_long_bg <- wb_data(my_indicators,
country = "world",
start_date = 2010, end_date = 2014,
return_wide = FALSE, lang = "bg"
)
## End(Not run)
World Bank Information End Points
Description
These functions are simple wrappers around the various useful API end points that are helpful for finding avaiable data and filtering the data you are interested in when using wb_data()
Usage
wb_countries(lang)
wb_topics(lang)
wb_sources(lang)
wb_regions(lang)
wb_income_levels(lang)
wb_lending_types(lang)
wb_languages()
Arguments
lang |
Language in which to return the results. If |
Value
A data.table/data.frame of information about the end point. A data.table is used instead of a plain data.frame so that large tables (e.g. the ~28k rows returned by wb_indicators()) print truncated to a handful of rows instead of flooding the console.
See Also
Download Avialable Indicators from the World Bank
Description
This function returns a data.frame of indicator IDs and related information that are available for download from the World Bank API
Usage
wb_indicators(lang, include_archive = FALSE)
Arguments
include_archive |
|
lang |
Language in which to return the results. If |
Examples
# can get a new list of available indicators by downloading new cache
fresh_cache <- wb_cache()
fresh_indicators <- fresh_cache$indicators
# or by running the wb_indicators() function directly
fresh_indicators <- wb_indicators()
# include archived indicators
# see include_archive parameter description
indicators_with_archive <- wb_indicators(include_archive = TRUE)
Search indicator information available through the World Bank API
Description
This function allows finds indicators that match a search term and returns a data frame of matching results
Usage
wb_search(
pattern,
fields = c("indicator_id", "indicator", "indicator_desc"),
extra = FALSE,
cache,
ignore.case = TRUE,
...
)
Arguments
pattern |
Character string or regular expression to be matched |
fields |
Character vector of column names through which to search |
extra |
if FALSE, only the indicator ID and short name are returned, if |
ignore.case |
if |
cache |
List of data frames returned from |
... |
Any additional |
Value
a data.table/data.frame with indicators that match the search pattern.
Examples
d <- wb_search(pattern = "education")
d <- wb_search(pattern = "Food and Agriculture Organization", fields = "source_org")
# with regular expression operators
# 'poverty' OR 'unemployment' OR 'employment'
d <- wb_search(pattern = "poverty|unemployment|employment")
# pass any other grep argument along as well
# everything without 'education'
d <- wb_search(pattern = "education", invert = TRUE)
# contains "gdp" AND "trade"
d <- wb_search("^(?=.*gdp)(?=.*trade).*", perl = TRUE)
# contains "gdp" and NOT "trade"
d <- wb_search("^(?=.*gdp)(?!.*trade).*", perl = TRUE)
Programmatic Access to Data and Statistics from the World Bank API
Description
Search and download data from the World Bank Data API. Includes support for mutliple languages, access to annual, quarterly, and monthly data.