R/prohibit_unequal_length_vectors.R

Defines functions prohibit_unequal_length_vectors

Documented in prohibit_unequal_length_vectors

#' Prohibit unequal length vectors
#' @description Tests whether all vectors have the same length.
#' @param ... Vectors to test.
#' @return An error message unless all of \code{...} have the same length in which case \code{NULL}, invisibly.
#' @export

prohibit_unequal_length_vectors <- function(...) {
  lengths <- lengths(list(...))
  if (max(lengths) != min(lengths)) {
    max.length <- max(lengths)
    i <- which.max(lengths != max.length)
    j <- which.max(lengths)
    dots <- eval(substitute(alist(...)))
    first_wrong_arg <- as.character(dots[i])
    max_wrong_arg <- as.character(dots[j])
    stop("`", first_wrong_arg, "` had length ", lengths[i],
         ", but the length of `", max_wrong_arg, "` is ", max.length, ". ",
         "Each vector must have the same length.")
  }
  invisible(NULL)
}

Try the hutils package in your browser

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

hutils documentation built on April 13, 2022, 5:23 p.m.