R/chk-length.R

Defines functions vld_length chk_length

Documented in chk_length vld_length

#' Check Length
#'
#' @description
#' Checks length is a particular value or range using
#'
#' `length(x) >= length && length(x) <= upper`
#'
#' @inheritParams params
#' @inherit params return
#'
#' @family chk_misc
#' @seealso [check_dim()]
#'
#' @examples
#' # chk_length
#' chk_length("text")
#' try(vld_length("text", length = 2))
#' @export
chk_length <- function(x, length = 1L, upper = length, x_name = NULL) {
  if (vld_length(x, length, upper)) {
    return(invisible(x))
  }
  if (is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
  if (length == upper) {
    abort_chk(x_name, " must be length ", length, " not ", length(x), x = x, length = length)
  }
  abort_chk(x_name, " must have a length between ", length, " and ", upper, " not ", length(x), x = x, length = length)
}

#' @describeIn chk_length Validate Length
#'
#' @examples
#' # vld_length
#' vld_length(2:1, 2)
#' vld_length(2:1, 1)
#' @export
vld_length <- function(x, length = 1L, upper = length) length(x) >= length && length(x) <= upper

Try the chk package in your browser

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

chk documentation built on Oct. 6, 2023, 9:06 a.m.