Skip to content

Commit

Permalink
feat: support for core languages and additional data cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
m-muecke committed Dec 11, 2023
1 parent 3659373 commit d0d585c
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 50 deletions.
94 changes: 56 additions & 38 deletions R/wb.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ wb_lang <- function() {
#' World Bank lending type data
#'
#' @param type character(1) lending type to query. Default is "all".
#' @param lang character(1) language to query. Default is "en".
#' @export
#' @examples
#' \dontrun{
#' wb_lending_type()
#' }
wb_lending_type <- function(type = "all") {
wb_lending_type <- function(type = "all", lang = "en") {
stopifnot(is.character(type) && length(type) == 1)
lang <- match.arg(lang, c("en", "fr", "es", "ar", "zh"))

resource <- sprintf("lendingType/%s", type)
resource <- sprintf("%s/lendingType/%s", lang, type)
res <- worldbank(resource, \(resp) {
data <- resp_body_json(resp)[[2]]
data.frame(
Expand All @@ -43,15 +45,17 @@ wb_lending_type <- function(type = "all") {
#' World Bank income level data
#'
#' @param income character(1) income level to query. Default is "all".
#' @param lang character(1) language to query. Default is "en".
#' @export
#' @examples
#' \dontrun{
#' wb_income_lvl()
#' }
wb_income_lvl <- function(income = "all") {
wb_income_lvl <- function(income = "all", lang = "en") {
stopifnot(is.character(income) && length(income) == 1)
lang <- match.arg(lang, c("en", "fr", "es", "ar", "zh"))

resource <- sprintf("incomeLevel/%s", income)
resource <- sprintf("%s/incomeLevel/%s", lang, income)
res <- worldbank(resource, \(resp) {
data <- resp_body_json(resp)[[2]]
data.frame(
Expand All @@ -66,108 +70,112 @@ wb_income_lvl <- function(income = "all") {
#' World Bank source data
#'
#' @param source character(1) source to query. Default is "all".
#' @param page integer(1) page number to query. Default is NULL.
#' @param lang character(1) language to query. Default is "en".
#' @export
#' @examples
#' \dontrun{
#' wb_source()
#' }
wb_source <- function(source = "all", page = NULL) {
wb_source <- function(source = "all", lang = "en") {
stopifnot(is.character(source) && length(source) == 1)
stopifnot(is_page(page))
lang <- match.arg(lang, c("en", "fr", "es", "ar", "zh"))

resource <- sprintf("source/%s", source)
resource <- sprintf("%s/source/%s", lang, source)
res <- worldbank(resource, \(resp) {
data <- resp_body_json(resp)[[2]]
data.frame(
id = map_chr(data, "id"),
last_updated = map_chr(data, "lastupdated"),
name = map_chr(data, "name"),
id = map_chr(data, "id") |> as.integer(),
last_updated = map_chr(data, "lastupdated") |> as.Date(),
name = map_chr(data, "name") |> na_if_empty(),
code = map_chr(data, "code"),
description = map_chr(data, "description") |> na_if_empty(),
url = map_chr(data, "url") |> na_if_empty(),
data_availability = map_chr(data, "dataavailability"),
metadata_availability = map_chr(data, "metadataavailability"),
concepts = map_chr(data, "concepts")
data_availability = map_chr(data, "dataavailability") |> to_logical(),
metadata_availability = map_chr(data, "metadataavailability") |>
to_logical(),
concepts = map_chr(data, "concepts") |> as.integer()
)
}, page = page)
}, page = 1)
as_tibble(res)
}

#' World Bank topic data
#'
#' @param topic character(1) topic to query. Default is "all".
#' @param page integer(1) page number to query. Default is NULL.
#' @param lang character(1) language to query. Default is "en".
#' @export
#' @examples
#' \dontrun{
#' wb_topic()
#' }
wb_topic <- function(topic = "all", page = NULL) {
wb_topic <- function(topic = "all", lang = "en") {
stopifnot(is.character(topic) && length(topic) == 1)
stopifnot(is_page(page))
lang <- match.arg(lang, c("en", "fr", "es", "ar", "zh"))

resource <- sprintf("topic/%s", topic)
resource <- sprintf("%s/topic/%s", lang, topic)
res <- worldbank(resource, \(resp) {
data <- resp_body_json(resp)[[2]]
data.frame(
id = map_chr(data, "id"),
id = map_chr(data, "id") |> as.integer(),
value = map_chr(data, "value"),
source_note = map_chr(data, "sourceNote")
)
}, page = page)
}, page = 1)
as_tibble(res)
}

#' World Bank region data
#'
#' @param region character(1) region to query. Default is "all".
#' @param page integer(1) page number to query. Default is NULL.
#' @param lang character(1) language to query. Default is "en".
#' @export
#' @examples
#' \dontrun{
#' wb_region()
#' }
wb_region <- function(region = "all", page = NULL) {
wb_region <- function(region = "all", lang = "en") {
stopifnot(is.character(region) && length(region) == 1)
stopifnot(is_page(page))
lang <- match.arg(lang, c("en", "fr", "es", "ar", "zh"))

resource <- sprintf("region/%s", region)
resource <- sprintf("%s/region/%s", lang, region)
res <- worldbank(resource, \(resp) {
data <- resp_body_json(resp)[[2]]
data.frame(
id = map_chr(data, "id") |> na_if_empty(),
code = map_chr(data, "code"),
iso2code = map_chr(data, "iso2code"),
name = map_chr(data, "name")
name = map_chr(data, "name") |> na_if_empty() |> trimws()
)
}, page = page)
}, page = 1)
as_tibble(res)
}

#' World Bank country data
#'
#' @param country character(1) country to query.
#' @param page integer(1) page number to query. Default is NULL.
#' @param lang character(1) language to query. Default is "en".
#' @export
#' @examples
#' \dontrun{
#' wb_country()
#' }
wb_country <- function(country = "all", page = NULL) {
wb_country <- function(country = "all", lang = "en") {
stopifnot(is.character(country) && length(country) == 1)
lang <- match.arg(lang, c("en", "fr", "es", "ar", "zh"))
stopifnot(is_page(page))

resource <- sprintf("country/%s", country)
resource <- sprintf("%s/country/%s", lang, country)
res <- worldbank(resource, \(resp) {
data <- resp_body_json(resp)[[2]]
data.frame(
country_id = map_chr(data, "id"),
country_code = map_chr(data, "iso2Code"),
country_name = map_chr(data, "name"),
country_name = map_chr(data, "name") |> na_if_empty(),
region_id = map_chr(data, \(x) x$region$id),
region_code = map_chr(data, \(x) x$region$iso2code),
region_value = map_chr(data, \(x) x$region$value) |> trimws(),
region_value = map_chr(data, \(x) x$region$value) |>
trimws() |>
na_if_empty(),
admin_region_id = map_chr(data, \(x) x$adminregion$id) |> na_if_empty(),
admin_region_code = map_chr(data, \(x) x$adminregion$iso2code) |>
na_if_empty(),
Expand All @@ -179,29 +187,32 @@ wb_country <- function(country = "all", page = NULL) {
lending_type_id = map_chr(data, \(x) x$lendingType$id) |> na_if_empty(),
lending_type_code = map_chr(data, \(x) x$lendingType$iso2code) |>
na_if_empty(),
lending_type_value = map_chr(data, \(x) x$lendingType$value),
lending_type_value = map_chr(data, \(x) x$lendingType$value) |>
na_if_empty(),
capital_city = map_chr(data, "capitalCity") |> na_if_empty(),
longitude = map_chr(data, "longitude") |> na_if_empty() |> as.numeric(),
latitude = map_chr(data, "latitude") |> na_if_empty() |> as.numeric()
)
}, page = page)
}, page = 1)
as_tibble(res)
}

#' World Bank indicator data
#'
#' @param indicator character(1) indicator to query. Default is "all".
#' @param lang character(1) language to query. Default is "en".
#' @param page integer(1) page number to query. Default is NULL.
#' @export
#' @examples
#' \dontrun{
#' wb_indicator()
#' }
wb_indicator <- function(indicator = "all", page = NULL) {
wb_indicator <- function(indicator = "all", lang = "en", page = NULL) {
stopifnot(is.character(indicator) && length(indicator) == 1)
lang <- match.arg(lang, c("en", "fr", "es", "ar", "zh"))
stopifnot(is_page(page))

resource <- sprintf("indicator/%s", indicator)
resource <- sprintf("%s/indicator/%s", lang, indicator)
res <- worldbank(resource, \(resp) {
data <- resp_body_json(resp)[[2]]
data.frame(
Expand Down Expand Up @@ -236,6 +247,7 @@ wb_indicator <- function(indicator = "all", page = NULL) {
#'
#' @param indicator character(1) indicator to query.
#' @param country character(1) country to query.
#' @param lang character(1) language to query. Default is "en".
#' @param page integer(1) page number to query. Default is NULL.
#' @export
#' @examples
Expand All @@ -244,14 +256,16 @@ wb_indicator <- function(indicator = "all", page = NULL) {
#' }
wb_country_indicator <- function(indicator = "NY.GDP.MKTP.CD",
country = "US",
lang = "en",
page = NULL) {
stopifnot(is.character(indicator) && length(indicator) == 1)
stopifnot(
is.character(country) && length(country) == 1 && nchar(country) %in% 2:3
)
lang <- match.arg(lang, c("en", "fr", "es", "ar", "zh"))
stopifnot(is_page(page))

resource <- sprintf("country/%s/indicator/%s", country, indicator)
resource <- sprintf("%s/country/%s/indicator/%s", lang, country, indicator)
res <- worldbank(resource, \(resp) {
data <- resp_body_json(resp)[[2]]
data <- lapply(data, \(x) {
Expand All @@ -271,7 +285,11 @@ wb_country_indicator <- function(indicator = "NY.GDP.MKTP.CD",
decimal = x$decimal
)
})
do.call(rbind, data)
data <- do.call(rbind, data)
data$year <- as.integer(data$year)
data$unit <- na_if_empty(data$unit)
data$obs_status <- na_if_empty(data$obs_status)
data
}, page = page)
as_tibble(res)
}
Expand Down
4 changes: 2 additions & 2 deletions man/wb_country.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion man/wb_country_indicator.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/wb_income_lvl.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/wb_indicator.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/wb_lending_type.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/wb_region.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/wb_source.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/wb_topic.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d0d585c

Please sign in to comment.