Skip to content

Commit

Permalink
lines longer than 80 characters
Browse files Browse the repository at this point in the history
  • Loading branch information
datapumpernickel committed Dec 22, 2023
1 parent 7916019 commit 1992476
Show file tree
Hide file tree
Showing 16 changed files with 395 additions and 181 deletions.
8 changes: 4 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ Authors@R: c(
role = "rev",
comment = c(ORCID = "0000-0002-3915-0618",
"Alicia reviewed the package for rOpenSci,
see https://github.com/ropensci/onboarding/issues/141")),
see https://github.com/ropensci/onboarding/issues/141")),
person("Rafael", "Hellwig",
role = "rev",
comment = c(ORCID = "0000-0002-3092-3493",
"Rafael reviewed the package for rOpenSci,
see https://github.com/ropensci/onboarding/issues/141")),
see https://github.com/ropensci/onboarding/issues/141")),
person("Ernest", "Guevarra",
role = "rev",
comment = c(ORCID = "0000-0002-4887-4415",
"Ernest reviewed the package for rOpenSci,
see https://github.com/ropensci/software-review/issues/613")),
see https://github.com/ropensci/software-review/issues/613")),
person("Nicholas", "Potter",
role = "rev",
comment = c(ORCID = "0000-0002-3410-3732",
"Nicholas reviewed the package for rOpenSci,
see https://github.com/ropensci/software-review/issues/613")),
see https://github.com/ropensci/software-review/issues/613")),
person("Juergen", "Amann", role=c("ctb")))
Description: Interface with and extract data from the United Nations Comtrade
API <https://comtradeplus.un.org/>. Comtrade provides country level shipping
Expand Down
10 changes: 6 additions & 4 deletions R/ct_build_request.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#' Build a valid request object from the checked parameters
#'
#' This is an internal function takes the necessary parameters from `ct_check_params()`
#' and creates a httr2 request to be performed. This request can then be used in a second function, `ct_perform_request()` to actually return the data.
#' It is called internally ct `ct_get_data()`
#' This is an internal function takes the necessary parameters
#' from `ct_check_params()` and creates a httr2 request to be performed.
#' This request can then be used in a second function, `ct_perform_request()`
#' to actually return the data. It is called internally ct `ct_get_data()`
#'
#' @param params a named vector of parameters for the comtrade request, result from `ct_check_params()`.
#' @param params a named vector of parameters for the comtrade request,
#' result from `ct_check_params()`.
#'
#' @examplesIf interactive()
#' # Build request from checked parameters
Expand Down
108 changes: 67 additions & 41 deletions R/ct_check_params.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#' Check that input parameters are valid and in compliance with UN Comtrade APIs.
#' Check input parameters are valid and in compliance with UN Comtrade APIs.
#'
#' This is an internal function that is called by `ct_get_data()` and processes
#' most arguments that are passed to it according to the relevant limitations of the
#' official Comtrade API.
#' most arguments that are passed to it according to the relevant limitations
#' of the official Comtrade API.
#'
#'
#' @returns Returns a list of named parameters for building a request.
Expand Down Expand Up @@ -220,10 +220,14 @@ check_clCode <- function(type, commodity_classification) {
#' @returns A character vector specifying the trade flow codes.
#'
#' @examplesIf interactive()
#' check_flowCode("import") # returns "M"
#' check_flowCode(c("export", "re-export")) # returns "X,RX"
#' check_flowCode("trade") # throws an error because "trade" is not a valid flow code
#' check_flowCode(NULL) # throws an error because at least one flow code must be provided
#' check_flowCode("import")
#' # returns "M"
#' check_flowCode(c("export", "re-export"))
#' # returns "X,RX"
#' check_flowCode("trade")
#' # throws an error because "trade" is not a valid flow code
#' check_flowCode(NULL)
#' # throws an error because at least one flow code must be provided
#'
#' @noRd
check_flowCode <- function(flow_direction, update, verbose) {
Expand All @@ -245,7 +249,7 @@ check_flowCode <- function(flow_direction, update, verbose) {
) |>
poorman::mutate(text = tolower(text))

rlang::arg_match(flow_direction, values = valid_codes$text, multiple = T)
rlang::arg_match(flow_direction, values = valid_codes$text, multiple = TRUE)

# if one of the codes is not in the list of valid codes
# send stop signal and list problems
Expand Down Expand Up @@ -275,10 +279,14 @@ check_flowCode <- function(flow_direction, update, verbose) {
#' @returns A character vector specifying the commodity codes requested.
#'
#' @examplesIf interactive()
#' check_cmdCode("01") # returns "01"
#' check_cmdCode(c("01", "02")) # returns "01,02"
#' check_cmdCode("ABC") # throws an error because "ABC" is not a valid HS code
#' check_cmdCode(NULL) # throws an error because at least one HS code must be provided
#' check_cmdCode("01")
#' # returns "01"
#' check_cmdCode(c("01", "02"))
#' # returns "01,02"
#' check_cmdCode("ABC")
#' # throws an error because "ABC" is not a valid HS code
#' check_cmdCode(NULL)
#' # throws an error because at least one HS code must be provided
#'
#' @noRd
check_cmdCode <-
Expand Down Expand Up @@ -320,18 +328,22 @@ check_cmdCode <-

#' Check validity of reporter parameter.
#'
#' This function checks that the given reporter code is valid. If the code is not
#' valid, the function throws an error message indicating which codes are invalid.
#' It also converts the input to a proper format if necessary.
#' This function checks that the given reporter code is valid.
#' If the code is not valid, the function throws an error message
#' indicating which codes are invalid. It also converts the input to a
#' proper format if necessary.
#'
#' @inheritParams ct_get_data
#'
#' @returns A character vector of valid reporter IDs.
#'
#' @examplesIf interactive()
#' check_reporterCode("USA") # returns "842,841"
#' check_reporterCode(c("USA", "FRA")) # returns "251,842,841"
#' check_reporterCode("all") # returns all country codes, excluding any country groupings
#' check_reporterCode("USA")
#' # returns "842,841"
#' check_reporterCode(c("USA", "FRA"))
#' # returns "251,842,841"
#' check_reporterCode("all")
#' # returns all country codes, excluding any country groupings
#'
#' @noRd
check_reporterCode <- function(reporter, update = FALSE, verbose = FALSE) {
Expand All @@ -353,7 +365,7 @@ check_reporterCode <- function(reporter, update = FALSE, verbose = FALSE) {
## get multiple values or single values that are not 'all_countries'
if (length(reporter) > 1 | !any(reporter == "all_countries")) {
if (any(reporter == "all_countries")) {
rlang::abort('"all_countries" can only be provided as a single argument.')
rlang::abort('"all_countries" can only be provided as a single argument.')
}
# if one of the reporter codes is not in the list of valid reporter codes
# send stop signal and list problems
Expand Down Expand Up @@ -386,18 +398,22 @@ check_reporterCode <- function(reporter, update = FALSE, verbose = FALSE) {

#' Check validity of partner parameter.
#'
#' This function checks that the given partner code is valid. If the code is not
#' valid, the function throws an error message indicating which codes are invalid.
#' It also converts the input to a proper format if necessary.
#' This function checks that the given partner code is valid.
#' If the code is not valid, the function throws an error message
#' indicating which codes are invalid. It also converts the input to a
#' proper format if necessary.
#'
#' @inheritParams ct_get_data
#'
#' @returns A character vector of valid partner IDs.
#'
#' @examplesIf interactive()
#' check_partnerCode("CAN") # returns "124"
#' check_partnerCode(c("CAN", "MEX")) # returns "124,484"
#' check_partnerCode("all") # returns all partner codes, excluding country groupings
#' check_partnerCode("CAN")
#' # returns "124"
#' check_partnerCode(c("CAN", "MEX"))
#' # returns "124,484"
#' check_partnerCode("all")
#' # returns all partner codes, excluding country groupings
#'
#' @noRd
check_partnerCode <- function(partner, update = FALSE, verbose = FALSE) {
Expand All @@ -417,9 +433,10 @@ check_partnerCode <- function(partner, update = FALSE, verbose = FALSE) {
if (length(partner) > 1 | !any(partner == "all_countries")) {
partner <- stringr::str_squish(partner)
if (any(partner == "all_countries")) {
rlang::abort('"all_countries" can only be provided as a single argument.')
rlang::abort('"all_countries" can only be provided as a single argument.')
}
# if one of the partnerCodes is not in the list of valid partnerCodes send stop signal and list problems
# if one of the partnerCodes is not in the list of valid partnerCodes
# send stop signal and list problems
if (!all(partner %in% partner_codes$iso_3)) {
rlang::abort(paste(
"The following partner you provided are invalid: ",
Expand Down Expand Up @@ -451,18 +468,22 @@ check_partnerCode <- function(partner, update = FALSE, verbose = FALSE) {

#' Check validity of partner_2 parameter.
#'
#' This function checks that the given partner_2 code is valid. If the code is not
#' valid, the function throws an error message indicating which codes are invalid.
#' It also converts the input to a proper format if necessary.
#' This function checks that the given partner_2 code is valid.
#' If the code is not valid, the function throws an error message
#' indicating which codes are invalid. It also converts the input
#' to a proper format if necessary.
#'
#' @inheritParams ct_get_data
#'
#' @returns A character vector of valid partner_2 IDs.
#'
#' @examplesIf interactive()
#' check_partner2Code("CAN") # returns "124"
#' check_partner2Code(c("CAN", "MEX")) # returns "124,484"
#' check_partner2Code("all") # returns all partner codes, excluding country groupings
#' check_partner2Code("CAN")
#' # returns "124"
#' check_partner2Code(c("CAN", "MEX"))
#' # returns "124,484"
#' check_partner2Code("all")
#' # returns all partner codes, excluding country groupings
#'
#' @noRd
check_partner2Code <- function(partner, update = FALSE, verbose = FALSE) {
Expand All @@ -482,9 +503,10 @@ check_partner2Code <- function(partner, update = FALSE, verbose = FALSE) {
if (length(partner) > 1 | !any(partner == "all_countries")) {
partner <- stringr::str_squish(partner)
if (any(partner == "all_countries")) {
rlang::abort('"all_countries" can only be provided as a single argument.')
rlang::abort('"all_countries" can only be provided as a single argument.')
}
# if one of the partnerCodes is not in the list of valid partnerCodes send stop signal and list problems
# if one of the partnerCodes is not in the list
#of valid partnerCodes send stop signal and list problems
if (!all(partner %in% partner_codes$iso_3)) {
rlang::abort(paste(
"The following partner_2 you provided are invalid: ",
Expand Down Expand Up @@ -553,7 +575,7 @@ check_motCode <-
if (!all(mode_of_transport %in% valid_codes$text)) {
rlang::abort(
paste0(
"The following mode_of_transport codes you provided are invalid: ",
"The following mode_of_transport codes you provided are invalid: ",
paste0(
setdiff(mode_of_transport, valid_codes$text),
collapse = ", "
Expand Down Expand Up @@ -621,21 +643,25 @@ check_customsCode <- function(customs_code, update = FALSE, verbose = FALSE) {
#'
#' This function checks that the given period code is valid. If the range or
#' format is not valid, the function throws an error message indicating which
#' codes are invalid. It also converts the input to the proper format if necessary.
#' codes are invalid. It also converts the input to the proper format if
#' necessary.
#'
#' @inheritParams ct_get_data
#'
#' @returns A character vector of valid reporter IDs.
#'
#' @examplesIf interactive()
#' check_date(2010, 2011, "A") # returns "2010,2011"
#' check_date(2010, 2011, "A") # returns "2010"
#' check_date("2010-01", "2010-07", "M") # returns "201001,201002,201003,201004,201005,201006,201007"
#' check_date(2010, 2011, "A")
#' # returns "2010,2011"
#' check_date(2010, 2011, "A")
#' # returns "2010"
#' check_date("2010-01", "2010-07", "M")
#' # returns "201001,201002,201003,201004,201005,201006,201007"
#'
#' @noRd
check_date <- function(start_date, end_date, frequency) {
if (is.null(start_date) | is.null(end_date)) {
rlang::abort("Please provide a start and end date for the period of interest.")
rlang::abort("Please provide a start and end date for the period of interest.")
}

start_date <- as.character(start_date)
Expand Down
13 changes: 9 additions & 4 deletions R/ct_perform_request.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#' Performs the request to the Comtrade API
#'
#' This function is internally called by `ct_get_data()` and performs the request constructed by `ct_build_request()` and returns an error body with the respective error returned by the Comtrade API.
#' By default throttles all requests to 1 request per 6 seconds, or 10 requests per minute, but it adjusts automatically if the
#' This function is internally called by `ct_get_data()` and performs the
#' request constructed by `ct_build_request()` and returns an error body with
#' the respective error returned by the Comtrade API.
#' By default throttles all requests to 1 request per 6 seconds,
#' or 10 requests per minute, but it adjusts automatically if the
#' API asks for longer waiting times.
#'
#' @param req a valid comtrade request built by the `ct_build_request()` function
#' @param req a valid comtrade request built by the `ct_build_request()`
#' function
#'
#' @returns JSON data from comtrade, data.frame with results or error codes.
#' @examplesIf interactive()
Expand All @@ -17,7 +21,8 @@
#' flow_direction = "import"
#' )
#' req <- httr2::last_request()
#' resp <- comtradr:::ct_perform_request(req, requests_per_second = 10 / 60, verbose = FALSE)
#' resp <- comtradr:::ct_perform_request(req,
#' requests_per_second = 10 / 60, verbose = FALSE)
#' @inheritParams ct_get_data
ct_perform_request <- function(req, requests_per_second, verbose = FALSE) {
if (verbose) {
Expand Down
11 changes: 8 additions & 3 deletions R/ct_process_response.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#' Processes the response object
#'
#' The function is internally called by `ct_get_data()` and processes the httr2response object returned by `ct_perform_request()`, it parses the JSON and adds the respective ISO codes for the reporter and partner countries, as well as the commodity code description.
#' The function is internally called by `ct_get_data()` and processes
#' the httr2response object returned by `ct_perform_request()`, it parses the
#' JSON and adds the respective ISO codes for the reporter and partner
#' countries, as well as the commodity code description.
#'
#' @param resp a valid httr2 response object created from the function `ct_perform_request()`
#' @param resp a valid httr2 response object created
#' from the function `ct_perform_request()`
#'
#' @returns a data.frame object with the results
#'
Expand All @@ -16,7 +20,8 @@
#' flow_direction = "import"
#' )
#' req <- httr2::last_request()
#' resp <- comtradr:::ct_perform_request(req, requests_per_second = 10 / 60, verbose = FALSE)
#' resp <- comtradr:::ct_perform_request(req, requests_per_second = 10 / 60,
#' verbose = FALSE)
#' data <- comtradr:::ct_process_response(resp)
#' @inheritParams ct_get_data
ct_process_response <- function(resp, verbose = FALSE, tidy_cols) {
Expand Down
26 changes: 18 additions & 8 deletions R/data.R
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
#' Country codes
#'
#' A full dataset of all reporter and partner codes available in the UN Comtrade database.
#' A full dataset of all reporter and partner codes available
#' in the UN Comtrade database.
#'
#' @format `country_codes`
#' A dataframe with 312 rows and eight columns:
#' \describe{
#' \item{id}{Unique country code.}
#' \item{country}{Name of the country (in English).}
#' \item{iso_3}{The country's ISO 3 code.}
#' \item{entry_year}{The country's entry into the international system or 1900 (whichever is largest).}
#' \item{exit_year}{The country's exit from the international system, if applicable.}
#' \item{group}{Indicates whether the entity is a group of countries. For example, ASEAN or the European Union.}
#' \item{reporter}{Indicates whether the country is a reporter in the UN Comtrade database.}
#' \item{partner}{Indicates whether the country can be reported on by others in the UN Comtrade database. Not all partners are reporters. For example, the World cannot report its trade values.}
#' \item{entry_year}{The country's entry into
#' the international system or 1900 (whichever is largest).}
#' \item{exit_year}{The country's exit from the
#' international system, if applicable.}
#' \item{group}{Indicates whether the entity is a
#' group of countries. For example, ASEAN or the European Union.}
#' \item{reporter}{Indicates whether the country is a
#' reporter in the UN Comtrade database.}
#' \item{partner}{Indicates whether the country can be
#' reported on by others in the UN Comtrade database. Not all
#' partners are reporters. For example,
#' the World cannot report its trade values.}
#' }
#' @source <https://comtradeapi.un.org/files/v1/app/reference/Reporters.json> and <https://comtradeapi.un.org/files/v1/app/reference/partnerAreas.json>
#' @source <https://comtradeapi.un.org/files/v1/app/reference/Reporters.json>
#' and <https://comtradeapi.un.org/files/v1/app/reference/partnerAreas.json>
"country_codes"

#' ct_pretty_cols
#'
#' A data.frame with a matched list of tidy and untidy column names for the results.
#' A data.frame with a matched list of tidy and
#' untidy column names for the results.
#'
#' @format `country_codes`
#' A dataframe with 47 rows and twi columns:
Expand Down
Loading

0 comments on commit 1992476

Please sign in to comment.