R/missing-colnames.R

#' Check Missing Colnames
#' 
#' Checks whether specific colnames are missing from a data frame.
#'
#' @param x The data to check.
#' @param colnames A character vector of the column names that must be missing from x.
#' @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_colnames}} and \code{\link{check_data}}
#' @export
#'
#' @examples
#' data <- data.frame(x = 1, y = 2, z = 0)
#' check_missing_colnames(data, c("y", "x", "a"), error = FALSE)
#' check_missing_colnames(data, "a", error = FALSE)
check_missing_colnames <- function(x, colnames, 
                                   x_name = substitute(x),
                                   error = TRUE) {
  
  x_name <- chk_deparse(x_name)
  
  check_vector(colnames, "", unique = TRUE, named = FALSE)
  check_flag_internal(error)

  if(!length(colnames)) return(invisible(x))

  x_colnames <- colnames(x)
  
  if(is.null(x_colnames)) chk_fail(x_name, " must have column names", error = error)
  
  colnames <- intersect(colnames, x_colnames)
  
  if(length(colnames)) {
    chk_fail(x_name, co_and(sort(colnames), " must not have column%s %c"), 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.