Skip to content

Commit

Permalink
New standalone-check_crucial_names (#796)
Browse files Browse the repository at this point in the history
  • Loading branch information
maurolepore authored Jun 17, 2024
1 parent 0059ead commit 5991bd5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 38 deletions.
50 changes: 50 additions & 0 deletions R/standalone-check_crucial_names.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# ---
# repo: 2DegreesInvesting/tiltIndicator
# file: standalone-check_crucial_names.R
# last-updated: 2024-06-15
# license: https://unlicense.org
# imports: [rlang, glue]
# ---

#' @importFrom rlang is_named
#' @importFrom rlang abort
#' @importFrom glue glue
NULL

#' Check if a named object contains expected names
#'
#' Based on fgeo.tool::check_crucial_names()
#'
#' @param x A named object.
#' @param expected_names String; expected names of `x`.
#'
#' @return Invisible `x`, or an error with informative message.
#'
#' Adapted from: https://github.com/RMI-PACTA/r2dii.match/blob/main/R/check_crucial_names.R
#'
#' @examples
#' x <- c(a = 1)
#' check_crucial_names(x, "a")
#' try(check_crucial_names(x, "bad"))
#' @noRd
check_crucial_names <- function(x, expected_names) {
stopifnot(is_named(x))
stopifnot(is.character(expected_names))

ok <- all(unique(expected_names) %in% names(x))
if (!ok) {
abort_missing_names(sort(setdiff(expected_names, names(x))))
}

invisible(x)
}

abort_missing_names <- function(missing_names) {
abort(
"missing_names",
message = glue(
"Must have missing names:
{paste0('`', missing_names, '`', collapse = ', ')}"
)
)
}
38 changes: 0 additions & 38 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,44 +45,6 @@ lowercase_characters <- function(data) {
mutate(data, across(where(is.character), tolower))
}

#' Check if a named object contains expected names
#'
#' Based on fgeo.tool::check_crucial_names()
#'
#' @param x A named object.
#' @param expected_names String; expected names of `x`.
#'
#' @return Invisible `x`, or an error with informative message.
#'
#' Adapted from: https://github.com/RMI-PACTA/r2dii.match/blob/main/R/check_crucial_names.R
#'
#' @examples
#' x <- c(a = 1)
#' check_crucial_names(x, "a")
#' try(check_crucial_names(x, "bad"))
#' @noRd
check_crucial_names <- function(x, expected_names) {
stopifnot(is_named(x))
stopifnot(is.character(expected_names))

ok <- all(unique(expected_names) %in% names(x))
if (!ok) {
abort_missing_names(sort(setdiff(expected_names, names(x))))
}

invisible(x)
}

abort_missing_names <- function(missing_names) {
abort(
"missing_names",
message = glue(
"Must have missing names:
{paste0('`', missing_names, '`', collapse = ', ')}"
)
)
}

add_risk_category <- function(data, low_threshold, high_threshold, ...) {
mutate(data, risk_category = categorize_risk(
.data$profile_ranking, .data$low_threshold, .data$high_threshold, ...
Expand Down

0 comments on commit 5991bd5

Please sign in to comment.