This utility function checks whether all expected columns are present in a given data frame. It is particularly useful for validating user input or internal data structures within a package, helping to prevent downstream errors caused by missing variables.

required_cols(df, expected, name)

Arguments

df

A data frame to be checked.

expected

A character vector of expected column names.

name

A character string indicating the name or source of the data frame (used in the error message).

Value

The function does not return anything. It throws an error if required columns are missing.

Examples

required_cols(data.frame(a = 1, b = 2), c("a", "b"), "example_df") # OK
if (FALSE) { # \dontrun{
required_cols(data.frame(a = 1), c("a", "b"), "example_df") # Error
} # }