R/utils.R

Defines functions check_columns check_data_frame has_unique_names is_count is_whole_number

# Internal helpers shared across the package. Not exported.

# TRUE if `x` is a single, non-missing whole number with `x >= min`.
is_whole_number <- function(x, min = -Inf) {
  is.numeric(x) && rlang::is_scalar_integerish(x, finite = TRUE) && x >= min
}

# TRUE if `x` is a single, non-missing, positive integer value.
is_count <- function(x) {
  is_whole_number(x, min = 1)
}

# TRUE if `x` has no missing, empty, or duplicated elements.
has_unique_names <- function(x) {
  !anyNA(x) && all(nzchar(x)) && !anyDuplicated(x)
}

check_data_frame <- function(
  x,
  arg,
  class,
  call = rlang::caller_env()
) {
  if (!is.data.frame(x)) {
    ec_abort(
      c(
        "{.arg {arg}} must be a data frame.",
        "x" = "You supplied {.obj_type_friendly {x}}."
      ),
      class = class,
      call = call
    )
  }
  invisible(x)
}

check_columns <- function(
  data,
  cols,
  arg = "data",
  call = rlang::caller_env()
) {
  missing <- setdiff(cols, names(data))
  if (length(missing)) {
    ec_abort(
      paste0(
        "{.arg {arg}} is missing required ",
        "{cli::qty(missing)}column{?s}: {.field {missing}}."
      ),
      class = "enrollcast_error_missing_columns",
      call = call
    )
  }
  invisible(data)
}

Try the enrollcast package in your browser

Any scripts or data that you put into this service are public.

enrollcast documentation built on Aug. 27, 2026, 1:08 a.m.