R/chk-unique.R

Defines functions vld_unique chk_unique

Documented in chk_unique vld_unique

#' Check Unique
#'
#' @description
#' Checks if unique using
#'
#' `!anyDuplicated(x, incomparables = incomparables)`
#'
#' @inheritParams params
#' @inherit params return
#'
#' @family chk_misc
#'
#' @examples
#' # chk_unique
#' chk_unique(c(NA, 2))
#' try(chk_unique(c(NA, NA, 2)))
#' chk_unique(c(NA, NA, 2), incomparables = NA)
#' @export
chk_unique <- function(x, incomparables = FALSE, x_name = NULL) {
  if (vld_unique(x, incomparables = incomparables)) {
    return(invisible(x))
  }
  if (is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
  abort_chk(x_name, " must be unique", x = x, incomparables = incomparables)
}

#' @describeIn chk_unique Validate Unique
#'
#' @examples
#' # vld_unique
#' vld_unique(NULL)
#' vld_unique(numeric(0))
#' vld_unique(c(NA, 2))
#' vld_unique(c(NA, NA, 2))
#' vld_unique(c(NA, NA, 2), incomparables = NA)
#' @export
vld_unique <- function(x, incomparables = FALSE) {
  !.anyDuplicated(x, incomparables = incomparables)
}

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.