Skip to contents

Checks if an argument is an object inheriting from the S3 class specified.

Usage

assert_s3_class(arg, class, optional = FALSE)

Arguments

arg

A function argument to be checked

class

The S3 class to check for

optional

Is the checked argument optional? If set to FALSE and arg is NULL then an error is thrown

Value

The function throws an error if arg is an object which does not inherit from class. Otherwise, the input is returned invisibly.

Examples

example_fun <- function(obj) {
  assert_s3_class(obj, "factor")
}

example_fun(as.factor(letters))

try(example_fun(letters))
#> Error in assert_s3_class(obj, "factor") : 
#>   `obj` must be an object of class 'factor' but is a character vector

try(example_fun(1:10))
#> Error in assert_s3_class(obj, "factor") : 
#>   `obj` must be an object of class 'factor' but is an integer vector