| vec_ptype | R Documentation |
vec_ptype() returns the unfinalised prototype of a
single vector.
vec_ptype_common() returns the common type of multiple vectors. By
default, this is finalised for immediate usage, but
can optionally be left unfinalised for advanced common type determination.
vec_ptype_show() nicely prints the common type of any number of inputs,
and is designed for interactive exploration.
vec_ptype(x, ..., x_arg = "", call = caller_env())
vec_ptype_common(
...,
.ptype = NULL,
.finalise = TRUE,
.arg = "",
.call = caller_env()
)
vec_ptype_show(...)
x |
A vector |
... |
For For |
x_arg |
Argument name for |
call, .call |
The execution environment of a currently
running function, e.g. |
.ptype |
If Alternatively, you can supply |
.finalise |
Should
|
.arg |
An argument name as a string. This argument will be mentioned in error messages as the input that is at the origin of a problem. |
vec_ptype() and vec_ptype_common() return a prototype
(a size-0 vector).
vec_ptype()vec_ptype() returns size 0 vectors potentially
containing attributes but no data. Generally, this is just
vec_slice(x, 0L), but some inputs require special
handling.
While you can't slice NULL, the prototype of NULL is
itself. This is because we treat NULL as an identity value in
the vec_ptype2() monoid.
The prototype of logical vectors that only contain missing values
is the special unspecified type, which can be coerced to any
other 1d type. This allows bare NAs to represent missing values
for any 1d vector type. Finalising this type
converts it from unspecified back to logical.
See internal-faq-ptype2-identity for more information about identity values.
vec_ptype() is a performance generic. It is not necessary to implement it
because the default method will work for any vctrs type. However the default
method builds around other vctrs primitives like vec_slice() which incurs
performance costs. If your class has a static prototype, you might consider
implementing a custom vec_ptype() method that returns a constant. This will
improve the performance of your class in many cases (common type imputation in particular).
Because it may contain unspecified vectors, the prototype returned by
vec_ptype() is said to be unfinalised. Call vec_ptype_finalise() to
finalise it.
vec_ptype_common()vec_ptype_common() first finds the prototype of each input, then
successively calls vec_ptype2() to find a common type. It returns a
finalised prototype by default, but can optionally be
left unfinalised for advanced common type determination.
vec_ptype()vec_slice() for returning an empty slice
vec_ptype_common()vec_ptype2()
vec_ptype_finalise()
# Unknown types ------------------------------------------
vec_ptype_show()
vec_ptype_show(NULL)
# Vectors ------------------------------------------------
vec_ptype_show(1:10)
vec_ptype_show(letters)
vec_ptype_show(TRUE)
vec_ptype_show(Sys.Date())
vec_ptype_show(Sys.time())
vec_ptype_show(factor("a"))
vec_ptype_show(ordered("a"))
# Matrices -----------------------------------------------
# The prototype of a matrix includes the number of columns
vec_ptype_show(array(1, dim = c(1, 2)))
vec_ptype_show(array("x", dim = c(1, 2)))
# Data frames --------------------------------------------
# The prototype of a data frame includes the prototype of
# every column
vec_ptype_show(iris)
# The prototype of multiple data frames includes the prototype
# of every column that in any data frame
vec_ptype_show(
data.frame(x = TRUE),
data.frame(y = 2),
data.frame(z = "a")
)
# Finalisation -------------------------------------------
# `vec_ptype()` and `vec_ptype2()` return unfinalised ptypes so that they
# can be coerced to any other type
vec_ptype(NA)
vec_ptype2(NA, NA)
# By default `vec_ptype_common()` finalises so that you can use its result
# directly in other vctrs functions
vec_ptype_common(NA, NA)
# You can opt out of finalisation to make it work like `vec_ptype()` and
# `vec_ptype2()` with `.finalise = FALSE`, but don't forget that you must
# call `vec_ptype_finalise()` manually if you do so!
vec_ptype_common(NA, NA, .finalise = FALSE)
vec_ptype_finalise(vec_ptype_common(NA, NA, .finalise = FALSE))
# This can be useful in rare scenarios, like including a separate `default`
# argument in the ptype computation
xs <- list(NA, NA)
default <- "a"
try(vec_ptype2(vec_ptype_common(!!!xs), default))
vec_ptype2(vec_ptype_common(!!!xs, .finalise = FALSE), default)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.