vec_cast() provides general coercions from one type of vector to another,
and along with
vec_ptype2() forms the foundation of the vctrs type system.
It should generally not be called by R users, but is important for R
vec_restore() is designed specifically for casting a bare
vector to the original type; it's useful when relying
the actual implementation.
vec_cast_common(...) casts a collection to
vectors to the same type.
vec_cast(x, to, ..., x_arg = "x", to_arg = "to") vec_cast_common(..., .to = NULL) vec_restore(x, to, ..., n = NULL) # S3 method for logical vec_cast(x, to, ...) # S3 method for integer vec_cast(x, to, ...) # S3 method for double vec_cast(x, to, ...) # S3 method for complex vec_cast(x, to, ...) # S3 method for raw vec_cast(x, to, ...) # S3 method for character vec_cast(x, to, ...) # S3 method for list vec_cast(x, to, ...)
Vectors to cast.
Type to cast to. If
Argument names for
The total size to restore to. This is currently passed by
A vector the same length as
x with the same type as
or an error if the cast is not possible. An error is generated if
information is lost when casting between compatible types (i.e. when
there is no 1-to-1 mapping for a specific value).
Casting is more flexible than coercion, and allows for the possibility of
information loss. This diagram summarises possible coercions.
from any type connected to another type, provided that the arrows are
followed in only one direction. For example you can cast from logical to
character, and list to time, but you can not cast from logical to datetime.
Most casts are not symmetric: you can cast all integers to doubles, but you can only cast a subset of doubles back to integers. If a cast is potentially lossy, an error will be shown whenever an actual loss occurs.
The rules for coercing from a list are fairly strict: each component of the
list must be of length 1, and must be coercible to type
to. This ensures
that a round-trip to and form list is possible, without opening the door
to very flexible list flattening (which should be the job of a more
vec_cast() dispatches on both arguments because casting depends on both
the type of
x and of
to. This is implemented by having methods of
vec_cast.integer() also be S3 generics, which call
vec_cast() dispatches on its second argument, so that the name
of the final method uses the same convention as
as.xyz() methods, i.e.
vec_cast.integer.double() casts double to integers, in the same way
Whenever you implement a
make sure to always provide
vec_default_cast() from that method.
vignette("s3-vector") for full details.
A restore is a specialised type of cast, primarily used in
NextMethod() or a C-level function that works on
the underlying data structure. A
vec_restore() method can make
the following assumptions about
It has the correct type.
It has the correct names.
It has the correct
It is unclassed. This way you can call vctrs generics with
without triggering an infinite loop of restoration.
The length may be different (for example after
been called), and all other attributes may have been lost. The
method should restore all attributes so that after restoration,
vec_restore(vec_data(x), x) yields
To understand the difference between
think about factors: it doesn't make sense to cast an integer to a factor,
NextMethod() or another low-level function has stripped attributes,
you still need to be able to restore them.
The default method copies across all attributes so you only need to provide your own method if your attributes require special care (i.e. they are dependent on the data in some way). When implementing your own method, bear in mind that many R users add attributes to track additional metadata that is important to them, so you should preserve any attributes that don't require special handling for your class.
# x is a double, but no information is lost vec_cast(1, integer())#>  1#> Error : Lossy cast from `x` <double> to `to` <integer>. #> * Locations: 2#> Error : Lossy cast from `x` <double> to `to` <logical>. #> * Locations: 2# You can suppress this error and get the partial results allow_lossy_cast(vec_cast(c(1, 1.5), integer()))#>  1 1#>  TRUE TRUE# By default this suppress all lossy cast errors without # distinction, but you can be specific about what cast is allowed # by supplying prototypes allow_lossy_cast(vec_cast(c(1, 1.5), integer()), to_ptype = integer())#>  1 1#> Error : Lossy cast from `x` <double> to `to` <logical>. #> * Locations: 2#> Error : Can't cast `x` <double> to `to` <factor<127a2>>.#> [] #>  a #> Levels: a b #> #> [] #>  a b #> Levels: a b #>#> [] #> [][] #>  a #> Levels: a #> #> #> [] #> [][] #>  "2019-10-21" #> #>