Check if the arguments of a binary operator have valid classes, and if not, generate an error message.

check_binary_classes(
  x,
  y,
  valid_x,
  valid_y = NULL,
  operator = NULL,
  commutative = NULL,
  general = NULL,
  specific = NULL,
  supplement = NULL,
  ...
)

Arguments

x, y

The argument to check, which can be any object.

valid_x, valid_y

A character vector which contains the valid classes. valid_y is assigned valid_x, if not specified.

operator

Optional. A single character which represents the binary operator.

commutative

TRUE or FALSE which indicates if arguments x and y can be swapped around. The default value is TRUE.

general

Optional. A single character which is used to give a general statement of the error incurred. By default, this is generated automatically.

specific

Optional. A single character which gives a detailed description of the error. glue::glue() syntax can be used, see "Examples" section. By default, this is generated automatically.

supplement

Optional. A (named) character vector which gives some additional information about the error. The names are used to create bullets, see throw(). By default, this is left empty.

...

Optional. Additional arguments which can be retrieved with tryCatch().

Value

returns an invisible NULL if the argument is valid, or generates an error message.

See also

"Examples" section in check_type() for how to customize error message and how to add and retrieve additional arguments.

vignette("erify") for a gentle introduction to this package.

Examples

if (FALSE) {
x <- 1
class(x) <- c("a", "b")

y <- 2
class(y) <- c("c", "d")

check_binary_classes(x, y, c("d", "e"))
check_binary_classes(x, y, c("d", "e"), operator = "+")

check_binary_classes(x, y, c("d", "e"), c("a", "f"))
check_binary_classes(x, y, c("d", "e"), c("a", "f"), commutative = FALSE)

# customize error message with `glue::glue()` syntax
check_binary_classes(
  x, y, c("d", "e"),
  specific = "Left: {feature_x[1]}, {feature_x[2]}.",
  supplement = "Right: {feature_y[1]}, {feature_y[2]}."
)
}