Skip to contents padding-top: 70px;

Checks whether the input table has the required variables.

Usage

check_required_variables(x, required_variables)

  # S3 method for class 'data.frame'
check_required_variables(x, required_variables)

  # S3 method for class 'exdf'
check_required_variables(x, required_variables)

Arguments

x

A table-like R object such as a data frame or an exdf.

required_variables

A set of variables that must each be included in x as columns.

Details

check_required_variables is generic, with methods defined for data frames and exdf objects.

When x is a data.frame, the required_variables input argument must be a character vector specifying the names of columns that should be included in x.

When x is an exdf, the required_variables input argument must be a list of named strings, where the name of each element specifies the name of a column that must be included in x, while the value of each column specifies the corresponding units for that column. If the value is NA, no unit checking will be performed.

If any required variables are missing, an informative error will be thrown. Otherwise, check_required_variables will have no output and produce no messages.

This function is used internally by many other functions from the PhotoGEA package to check for important columns and make sure they have the correct units. For example, see the code for apply_gm by typing PhotoGEA::apply_gm in the R terminal.

See also

Examples

# Create a simple exdf object
simple_exdf <- exdf(
  data.frame(A = c(3, 2, 7, 9), B = c(4, 5, 1, 8)),
  data.frame(A = 'm', B = 's', stringsAsFactors = FALSE),
  data.frame(A = 'Cat1', B = 'Cat2', stringsAsFactors = FALSE)
)

# Confirm that columns named `A` and `B` are in the object, and that they have
# units of `m` and `s`, respectively.
check_required_variables(simple_exdf, list(A = 'm', B = 's'))

# Confirm that columns named `A` and `B` are in the object, but only check units
# for the `A` column.
check_required_variables(simple_exdf, list(A = 'm', B = NA))

# Use the data frame method on `simple_exdf$main_data` to confirm that columns
# named `A` and `B` are present
check_required_variables(simple_exdf$main_data, c('A', 'B'))