R/nlevels.R

#' Check nlevels
#' 
#' Checks the number of levels of an object.
#'
#' @param x The data to check.
#' @param nlevels A flag indicating whether x should have elements (versus no elements) or a missing value indicating no requirements or a count or count range of the number of elements.
#' @param x_name A string of the name of the object.
#' @param error A flag indicating whether to throw an informative error or immediately generate an informative message if the check fails.
#' @return An invisible copy of x (if it doesn't throw an error).
#' @seealso \code{\link{check_levels}} and \code{\link{check_vector}} 
#' @export
#'
#' @examples
#' check_nlevels(factor(1), error = FALSE)
#' check_nlevels(factor(1), nlevels = 2, error = FALSE)
check_nlevels <- function(x,
                         nlevels = TRUE,
                         x_name = substitute(x),
                         error = TRUE) {
  x_name <- chk_deparse(x_name)

  check_length_internal(nlevels)

  check_flag_internal(error)
  check_n(x, n = nlevels(x), range = nlevels, x_name = x_name, n_name = "level", error = error)
  invisible(x)
}

Try the checkr package in your browser

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

checkr documentation built on May 1, 2019, 6:59 p.m.