Validates that the value belongs to a given class. One verb covers the
types without a dedicated check, including factor, Date, POSIXct,
list, matrix, environment, and fitted-model objects such as lm.
Arguments
- restriction
a
restrictionobject.- class
character(1) class name to require.
- exact
logical; if
TRUE, requiresclass(value)[1]to equalclassexactly. IfFALSE(default), tests inheritance withinherits(), so a subclass passes.
Details
A matrix is require_class("matrix") (inherits() is TRUE for
matrices since R 4.0) and an environment is
require_class("environment"); neither needs a dedicated step. Use
require_dim() for the shape.
See also
Other type checks:
require_character(),
require_df(),
require_integer(),
require_logical(),
require_numeric()
Examples
restrict("d") |> require_class("Date")
#> <restriction d>
#> 1. must be of class "Date"
restrict("f") |> require_class("factor")
#> <restriction f>
#> 1. must be of class "factor"
restrict("model") |> require_class("lm")
#> <restriction model>
#> 1. must be of class "lm"
restrict("m") |> require_class("matrix") |> require_dim(c(NA, 3))
#> <restriction m>
#> 1. must be of class "matrix"
#> 2. must have dim (any, 3)