| convert_lazy | R Documentation |
convert_lazy() is a non-strict variant of convert() that guarantees
that the result inherits from to, without forcing from to become an exact
instance of to, i.e. it never upcasts.
convert_lazy(from, to, ...)
from |
An S7 object to convert. |
to |
An S7 class specification, passed to |
... |
Other arguments passed to custom |
from, unchanged, if it already inherits from to; otherwise the
result of convert(from, to, ...).
convert() for the strict variant that always returns an exact
instance of to.
Foo1 := new_class(properties = list(x = class_integer))
Foo2 := new_class(Foo1, properties = list(y = class_double))
# `convert()` upcasts by stripping the extra properties of `from`:
convert(Foo2(x = 1L, y = 2), to = Foo1)
# `convert_lazy()` never upcasts: because the object already inherits from
# Foo1, it's returned unchanged, keeping `y`:
convert_lazy(Foo2(x = 1L, y = 2), to = Foo1)
# When `from` doesn't inherit from `to`, `convert_lazy()` falls back to
# `convert()`, so it can still downcast or coerce to a base type:
convert_lazy(Foo1(x = 1L), to = Foo2, y = 2.5)
convert_lazy(1.5, to = class_character)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.