diff --git a/R/wb.R b/R/wb.R index 7d1a8fe..a9b266a 100644 --- a/R/wb.R +++ b/R/wb.R @@ -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( @@ -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( @@ -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(), @@ -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( @@ -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 @@ -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) { @@ -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) } diff --git a/man/wb_country.Rd b/man/wb_country.Rd index dc8846c..92d397f 100644 --- a/man/wb_country.Rd +++ b/man/wb_country.Rd @@ -4,12 +4,12 @@ \alias{wb_country} \title{World Bank country data} \usage{ -wb_country(country = "all", page = NULL) +wb_country(country = "all", lang = "en") } \arguments{ \item{country}{character(1) country to query.} -\item{page}{integer(1) page number to query. Default is NULL.} +\item{lang}{character(1) language to query. Default is "en".} } \description{ World Bank country data diff --git a/man/wb_country_indicator.Rd b/man/wb_country_indicator.Rd index 93fbff7..c51ddf8 100644 --- a/man/wb_country_indicator.Rd +++ b/man/wb_country_indicator.Rd @@ -4,13 +4,20 @@ \alias{wb_country_indicator} \title{World Bank country indicator data} \usage{ -wb_country_indicator(indicator = "NY.GDP.MKTP.CD", country = "US", page = NULL) +wb_country_indicator( + indicator = "NY.GDP.MKTP.CD", + country = "US", + lang = "en", + page = NULL +) } \arguments{ \item{indicator}{character(1) indicator to query.} \item{country}{character(1) country to query.} +\item{lang}{character(1) language to query. Default is "en".} + \item{page}{integer(1) page number to query. Default is NULL.} } \description{ diff --git a/man/wb_income_lvl.Rd b/man/wb_income_lvl.Rd index 8582646..4f10dca 100644 --- a/man/wb_income_lvl.Rd +++ b/man/wb_income_lvl.Rd @@ -4,10 +4,12 @@ \alias{wb_income_lvl} \title{World Bank income level data} \usage{ -wb_income_lvl(income = "all") +wb_income_lvl(income = "all", lang = "en") } \arguments{ \item{income}{character(1) income level to query. Default is "all".} + +\item{lang}{character(1) language to query. Default is "en".} } \description{ World Bank income level data diff --git a/man/wb_indicator.Rd b/man/wb_indicator.Rd index 139f7cc..3a032fb 100644 --- a/man/wb_indicator.Rd +++ b/man/wb_indicator.Rd @@ -4,11 +4,13 @@ \alias{wb_indicator} \title{World Bank indicator data} \usage{ -wb_indicator(indicator = "all", page = NULL) +wb_indicator(indicator = "all", lang = "en", page = NULL) } \arguments{ \item{indicator}{character(1) indicator to query. Default is "all".} +\item{lang}{character(1) language to query. Default is "en".} + \item{page}{integer(1) page number to query. Default is NULL.} } \description{ diff --git a/man/wb_lending_type.Rd b/man/wb_lending_type.Rd index af4b016..94aebb3 100644 --- a/man/wb_lending_type.Rd +++ b/man/wb_lending_type.Rd @@ -4,10 +4,12 @@ \alias{wb_lending_type} \title{World Bank lending type data} \usage{ -wb_lending_type(type = "all") +wb_lending_type(type = "all", lang = "en") } \arguments{ \item{type}{character(1) lending type to query. Default is "all".} + +\item{lang}{character(1) language to query. Default is "en".} } \description{ World Bank lending type data diff --git a/man/wb_region.Rd b/man/wb_region.Rd index 9af014b..d21ee6d 100644 --- a/man/wb_region.Rd +++ b/man/wb_region.Rd @@ -4,12 +4,12 @@ \alias{wb_region} \title{World Bank region data} \usage{ -wb_region(region = "all", page = NULL) +wb_region(region = "all", lang = "en") } \arguments{ \item{region}{character(1) region to query. Default is "all".} -\item{page}{integer(1) page number to query. Default is NULL.} +\item{lang}{character(1) language to query. Default is "en".} } \description{ World Bank region data diff --git a/man/wb_source.Rd b/man/wb_source.Rd index a5e40a0..afc3838 100644 --- a/man/wb_source.Rd +++ b/man/wb_source.Rd @@ -4,12 +4,12 @@ \alias{wb_source} \title{World Bank source data} \usage{ -wb_source(source = "all", page = NULL) +wb_source(source = "all", lang = "en") } \arguments{ \item{source}{character(1) source to query. Default is "all".} -\item{page}{integer(1) page number to query. Default is NULL.} +\item{lang}{character(1) language to query. Default is "en".} } \description{ World Bank source data diff --git a/man/wb_topic.Rd b/man/wb_topic.Rd index 5bfee4f..55ef1ef 100644 --- a/man/wb_topic.Rd +++ b/man/wb_topic.Rd @@ -4,12 +4,12 @@ \alias{wb_topic} \title{World Bank topic data} \usage{ -wb_topic(topic = "all", page = NULL) +wb_topic(topic = "all", lang = "en") } \arguments{ \item{topic}{character(1) topic to query. Default is "all".} -\item{page}{integer(1) page number to query. Default is NULL.} +\item{lang}{character(1) language to query. Default is "en".} } \description{ World Bank topic data