R/check_crucial_names.R

Defines functions check_crucial_names

Documented in check_crucial_names

#' Check if a named object contains expected names
#'
#' It is based on:
#' <https://www.rdocumentation.org/packages/fgeo.tool/versions/1.2.5/topics/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.
#' @export
#'
#' @family miscellaneous utility functions
#'
#' @examples
#' v <- c(name_a = 1)
#' check_crucial_names(v, "name_a")
#' try(check_crucial_names(v, "name_b"))
#'
#' df <- data.frame(name_a = 1)
#' check_crucial_names(df, "name_a")
#' try(check_crucial_names(df, "name_b"))
check_crucial_names <- function(x, expected_names) {
  stopifnot(rlang::is_named(x))
  stopifnot(is.character(expected_names))

  ok <- all(unique(expected_names) %in% names(x))
  if (ok) {
    return(invisible(x))
  }

  missing_names <- sort(setdiff(expected_names, names(x)))
  rlang::abort(glue::glue(
    "The data must have all expected names.
    Expected but missing: {usethis::ui_field(missing_names)}"
  ))
}
2DegreesInvesting/r2dii.utils documentation built on Jan. 23, 2020, 1:38 a.m.