Skip to contents

Check if argument is of proper class.

Usage

check_class(x, arg_class, alt_null = FALSE, add_msg = NULL)

Arguments

x

Function argument that is being asserted.

arg_class

Class name. Usually "character", "numeric", "data.frame", etc.

alt_null

Logical. Should argument accept NULL value.

add_msg

Is an additional message that can be printed over the standard function error message. You can:

  • pass the names of the arguments that failed the test by using {x_names} in the message body (e.g. "What are the {x_names}");

  • pass the tested class by using {arg_class} in the message body (e.g. "I want them to be {arg_class})"

  • pass the classes of the arguments that failed the test by using {wrong_class} in the message body (e.g. "{wrong_class} is wrong")

Value

If argument class is same as arg_class it returns invisible NULL. Otherwise the function throws an error.

Examples

c1 <- c("x", "y")
n1 <- c(1,3,4)
n2 <- c(1.5, 2.5)
i1 <- 1L
df1 <- data.frame(x = 1:5, y = 6:10)
new_class <- structure("new class", class= c("character", "new class"))
nl1 <- NULL
if (FALSE) { # \dontrun{
check_class(c1, arg_class = "character")
check_class(c1, arg_class = "numeric")
check_class(df1, arg_class = "data.frame")
check_class(
  new_class, arg_class = "tbl_df",
  add_msg = "{.arg {x_name}} with {.cls {wrong_class}} not {.cls {arg_class}}"
)
check_class(nl1, arg_class = "character")
check_class(nl1, arg_class = "character", alt_null = TRUE)
check_class(n2, arg_class = "character", alt_null = TRUE)
} # }