R/check-key.R

Defines functions check_key

Documented in check_key

#' Check Key
#'
#' Checks that columns in a data frame represent a unique key.
#'
#' By default (\code{key = NULL}) all the columns are checked.
#'
#' @inheritParams check_data_frame
#' @param key A character vector of the column names representing the key.
#'
#' @return Throws an informative error or returns an invisible copy of
#' the data.
#' @export
#'
check_key <- function(data, key = NULL, data_name = substitute(data)) {
  if (!is.character(data_name)) data_name <- deparse(data_name)
  data <- check_data_frame(data, data_name)
  data <- check_colnames(data, colnames = key, exclusive = FALSE,
                         ordered = FALSE, data_name = data_name)

  if (is.null(key)) key <- colnames(data)

  key <- as.character(key)
  if (anyDuplicated(data[key])) {
      error(plural("column", length(key), " "),
                 punctuate(key, "and"), " in ", data_name, " must be a unique key")
  }
  invisible(data)
}

Try the datacheckr package in your browser

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

datacheckr documentation built on May 30, 2017, 6:05 a.m.