Skip to contents

Asserts that a data frame contains the specified columns.

Usage

assert_cols(
  data,
  cols = NULL,
  strict = FALSE,
  obj_name = checkmate::vname(data)
)

Arguments

data

Data frame to check.

cols

Column names which must be present in data. A character vector or NULL.

strict

Whether additional columns not specified in cols are allowed in data.

obj_name

Name of the checked object to print in error messages.

Value

data, invisibly.

See also

Other data frame / tibble functions: is_equal_df(), reduce_df_list()

Examples

pal::assert_cols(data = mtcars,
                 cols = c("mpg", "disp"))

try(
  pal::assert_cols(data = mtcars,
                   cols = c("mpg", "display"))
)
#> Error in pal::assert_cols(data = mtcars, cols = c("mpg", "display")) : 
#>   Assertion on 'mtcars' failed: Column names must include the elements {'mpg','display'}, but is missing elements {'display'}.

try(
  pal::assert_cols(data = mtcars,
                   cols = c("mpg", "disp"),
                   strict = TRUE)
)
#> Error in pal::assert_cols(data = mtcars, cols = c("mpg", "disp"), strict = TRUE) : 
#>   Assertion on 'mtcars' failed: Column names must be a identical to set {'mpg','disp'}, but is {'mpg','cyl','disp','hp','drat','wt','qsec','vs','am','gear','carb'}.

try(
  pal::assert_cols(data = mtcars,
                   strict = TRUE)
)
#> Error in pal::assert_cols(data = mtcars, strict = TRUE) : 
#>   Assertion on 'mtcars' failed: Column names must be a identical to set {}, but is {'mpg','cyl','disp','hp','drat','wt','qsec','vs','am','gear','carb'}.