destructure | R Documentation |
destructure
is used during unpacking assignment to coerce an object into a
list. Individual elements of the list are assigned to names on the left-hand
side of the unpacking assignment expression.
destructure(x)
## S3 method for class 'data.frame'
destructure(x)
## S3 method for class 'summary.lm'
destructure(x)
## Default S3 method:
destructure(x)
x |
An R object. |
New implementations of destructure
can be very simple. A new destructure
implementation might only strip away the class of a custom object and return
the underlying list structure. Alternatively, an object might destructure
into a nested set of values and may require a more complicated
implementation. In either case, new implementations must return a list object
so %<-%
can handle the returned value(s).
%<-%
# Data frames become a list of columns
destructure(faithful)
# A simple shape class
shape <- function(sides = 4, color = "red") {
structure(
list(sides = sides, color = color),
class = "shape"
)
}
## Not run:
# Cannot destructure the shape object _yet_
c(sides, color) %<-% shape()
## End(Not run)
# Implement a new destructure method for the shape class
destructure.shape <- function(x) {
unclass(x)
}
# Now we can destructure shape objects
c(sides, color) %<-% shape()
c(sides, color) %<-% shape(3, "green")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.