This abstract class provides a set of useful default methods that makes it
considerably easier to get started with a new S3 vector class. See
vignette("s3-vector")
to learn how to use it to create your own S3
vector classes.
new_vctr(.data, ..., class = character(), inherit_base_type = NULL)
Foundation of class. Must be a vector
Name-value pairs defining attributes
Name of subclass.
A single logical, or NULL
. Does this class extend the base type of
.data
? i.e. does the resulting object extend the behaviour of the
underlying type? Defaults to FALSE
for all types except lists, which
are required to inherit from the base type.
The vctr class provides methods for many base generics using a smaller set of generics defined by this package. Generally, you should think carefully before overriding any of the methods that vctrs implements for you as they've been carefully planned to be internally consistent.
[[
and [
use NextMethod()
dispatch to the underlying base function,
then restore attributes with vec_restore()
.
rep()
and length<-
work similarly.
[[<-
and [<-
cast value
to same type as x
, then call
NextMethod()
.
as.logical()
, as.integer()
, as.numeric()
, as.character()
,
as.Date()
and as.POSIXct()
methods call vec_cast()
.
The as.list()
method calls [[
repeatedly, and the as.data.frame()
method uses a standard technique to wrap a vector in a data frame.
as.factor()
, as.ordered()
and as.difftime()
are not generic functions
in base R, but have been reimplemented as generics in the generics
package. vctrs
extends these and calls vec_cast()
. To inherit this
behaviour in a package, import and re-export the generic of interest
from generics
.
==
, !=
, unique()
, anyDuplicated()
, and is.na()
use
vec_proxy()
.
<
, <=
, >=
, >
, min()
, max()
, range()
, median()
,
quantile()
, and xtfrm()
methods use vec_proxy_compare()
.
+
, -
, /
, *
, ^
, %%
, %/%
, !
, &
, and |
operators
use vec_arith()
.
Mathematical operations including the Summary group generics (prod()
,
sum()
, any()
, all()
), the Math group generics (abs()
, sign()
,
etc), mean()
, is.nan()
, is.finite()
, and is.infinite()
use vec_math()
.
dims()
, dims<-
, dimnames()
, dimnames<-
, levels()
, and
levels<-
methods throw errors.
List vctrs are special cases. When created through new_vctr()
, the
resulting list vctr should always be recognized as a list by
obj_is_list()
. Because of this, if inherit_base_type
is FALSE
an error is thrown.