R/check-df-names.R

Defines functions check_df_names

Documented in check_df_names

#' Function to check the names of a dataframe.
#'
#' This function is used in most plotting routines to check if the correct dataframe
#' is passed.
#'
#' @param data Dataframe to check.
#' @param expect Character vector giving the names of the expected columns
#' @param optional Character vector giving the names of optional columns.
#' Default is \code{NULL}.
#' @export
#'
#' @examples \dontrun{
#' check_df_names(preprocess$biomass_age, expect = c("time", "species", "atoutput", "ages"))
#' }

check_df_names <- function(data, expect, optional = NULL) {
  df_names <- names(data)[!is.element(names(data), optional)]
  wrong_names <- df_names[!is.element(df_names, expect)]

  if (length(wrong_names) >= 1) {
    stop(paste("Wrong column names in data:", paste(wrong_names, collapse = " & ")))
  }
}

Try the atlantistools package in your browser

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

atlantistools documentation built on Aug. 16, 2017, 9:05 a.m.