-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0cd0965
commit 7236888
Showing
12 changed files
with
416 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
linters: linters_with_defaults() # see vignette("lintr") | ||
encoding: "UTF-8" | ||
encoding: "UTF-8" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,16 @@ | ||
# Generated by roxygen2: do not edit by hand | ||
|
||
export(uis_get) | ||
export(uis_get_entities) | ||
export(uis_get_versions) | ||
export(uis_get_versions_default) | ||
import(cli) | ||
import(httr2) | ||
import(jsonlite) | ||
import(tibble) | ||
importFrom(dplyr,filter) | ||
importFrom(dplyr,mutate) | ||
importFrom(dplyr,rename) | ||
importFrom(dplyr,select) | ||
importFrom(tidyr,nest) | ||
importFrom(tidyr,unnest) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,131 @@ | ||
#' Get data from the UIS API | ||
#' | ||
#' @description | ||
#' Retrieves data from the UNESCO Institute for Statistics (UIS) API for | ||
#' specified entities, indicators, and time periods. | ||
#' | ||
#' @param entities Character vector. The entity IDs (geoUnits) to retrieve data | ||
#' for. Must provide either this parameter or `indicators` or both. | ||
#' @param indicators Character vector. The indicator IDs to retrieve data for. | ||
#' Must provide either this parameter or `entities` or both. | ||
#' @param start_year Numeric or character. The starting year for the data | ||
#' retrieval period. If NULL, no start year constraint is applied. | ||
#' @param end_year Numeric or character. The ending year for the data retrieval | ||
#' period. If NULL, no end year constraint is applied. | ||
#' @param version Character. The API version to use. If NULL, the default | ||
#' version is used. | ||
#' | ||
#' @return A data frame with the following columns: | ||
#' \item{entity_id}{Character. The ID of the entity (geoUnit).} | ||
#' \item{indicator_id}{Character. The ID of the indicator.} | ||
#' \item{year}{Numeric. The year of the observation.} | ||
#' \item{value}{Numeric. The value of the indicator for the given entity and | ||
#' year.} | ||
#' | ||
#' @examplesIf curl::has_internet() | ||
#' \donttest{ | ||
#' # Get a single indicator for all countries | ||
#' literacy_data <- uis_get( | ||
#' indicators = "200101", | ||
#' start_year = 2015, | ||
#' end_year = 2020 | ||
#' ) | ||
#' | ||
#' # Get multiple indicators for a single country | ||
#' uis_get( | ||
#' entities = "BRA", | ||
#' indicators = c("GER.1", "NER.1", "CR.1"), | ||
#' start_year = 2010, | ||
#' end_year = 2020 | ||
#' ) | ||
#' } | ||
#' | ||
#' @seealso | ||
#' \link{uis_get_entities} for retrieving available geographical entities and | ||
#' \link{uis_get_versions} for retrieving available API versions | ||
#' | ||
#' @export | ||
uis_get <- function( | ||
entities = NULL, | ||
indicators = NULL, | ||
start_year = NULL, | ||
end_year = NULL, | ||
version = NULL | ||
) { | ||
# TODO: at least entities or indicators must be given | ||
if (is.null(entities) && is.null(indicators)) { | ||
cli::cli_abort( | ||
c( | ||
"!" = "At least {.arg entities} or {.arg indicators} must be provided." | ||
) | ||
) | ||
} | ||
|
||
# TODO: add parameter validation | ||
validate_character_vector(indicators, "indicators") | ||
validate_character_vector(entities, "entities") | ||
validate_year(start_year, "end_year") | ||
validate_year(end_year, "end_year") | ||
validate_version(version) | ||
|
||
# TODO: add support for parameters | ||
resp <- perform_request("data/indicators") | ||
resp <- perform_request( | ||
"data/indicators", | ||
entities = entities, | ||
indicators = indicators, | ||
start_year = start_year, | ||
end_year = end_year, | ||
version = version | ||
) | ||
|
||
data_raw <- resp |> | ||
resp_body_json(simplifyVector = TRUE) | ||
|
||
data <- data_raw$records |> | ||
as_tibble() |> | ||
dplyr::select( | ||
entity_id = geoUnit, | ||
indicator_id = indicatorId, | ||
year, | ||
value | ||
records <- data_raw$records | ||
|
||
print(length(records)) | ||
if (length(records) == 0) { | ||
cli::cli_inform( | ||
c("i" = "No records found for this query.") | ||
) | ||
return(invisible(NULL)) | ||
} else { | ||
data <- records |> | ||
as_tibble() |> | ||
dplyr::select( | ||
"entity_id" = "geoUnit", | ||
"indicator_id" = "indicatorId", | ||
"year", | ||
"value" | ||
) | ||
return(data) | ||
} | ||
} | ||
|
||
#' @noRd | ||
#' @keywords internal | ||
validate_character_vector <- function(arg, arg_name) { | ||
if (!is.null(arg)) { | ||
if (!is.character(arg) || any(is.na(arg))) { | ||
cli::cli_abort( | ||
c( | ||
"!" = paste( | ||
"{.arg {arg_name}} must be a character vector and cannot contain ", | ||
"NA values." | ||
) | ||
) | ||
) | ||
} | ||
} | ||
} | ||
|
||
data | ||
#' @noRd | ||
#' @keywords internal | ||
validate_year <- function(year, arg_name) { | ||
if (!is.null(year)) { | ||
if (!is.numeric(year)) { | ||
cli::cli_abort( | ||
c( | ||
"!" = paste("{.arg {arg_name}} must be an integer.") | ||
) | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.