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.
Usage
new_vctr(.data, ..., class = character(), inherit_base_type = NULL)
Arguments
- .data
Foundation of class. Must be a vector
- ...
Name-value pairs defining attributes
- class
Name of subclass.
- inherit_base_type
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 toFALSE
for all types except lists, which are required to inherit from the base type.
Details
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.
Base methods
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[
useNextMethod()
dispatch to the underlying base function, then restore attributes withvec_restore()
.rep()
andlength<-
work similarly.[[<-
and[<-
castvalue
to same type asx
, then callNextMethod()
.as.logical()
,as.integer()
,as.numeric()
,as.character()
,as.Date()
andas.POSIXct()
methods callvec_cast()
. Theas.list()
method calls[[
repeatedly, and theas.data.frame()
method uses a standard technique to wrap a vector in a data frame.as.factor()
,as.ordered()
andas.difftime()
are not generic functions in base R, but have been reimplemented as generics in thegenerics
package.vctrs
extends these and callsvec_cast()
. To inherit this behaviour in a package, import and re-export the generic of interest fromgenerics
.==
,!=
,unique()
,anyDuplicated()
, andis.na()
usevec_proxy()
.<
,<=
,>=
,>
,min()
,max()
,range()
,median()
,quantile()
, andxtfrm()
methods usevec_proxy_compare()
.+
,-
,/
,*
,^
,%%
,%/%
,!
,&
, and|
operators usevec_arith()
.Mathematical operations including the Summary group generics (
prod()
,sum()
,any()
,all()
), the Math group generics (abs()
,sign()
, etc),mean()
,is.nan()
,is.finite()
, andis.infinite()
usevec_math()
.dims()
,dims<-
,dimnames()
,dimnames<-
,levels()
, andlevels<-
methods throw errors.