destructure: Destructure an object

View source: R/destructure.R

destructureR Documentation

Destructure an object

Description

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.

Usage

destructure(x)

## S3 method for class 'data.frame'
destructure(x)

## S3 method for class 'summary.lm'
destructure(x)

## Default S3 method:
destructure(x)

Arguments

x

An R object.

Details

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).

See Also

%<-%

Examples


# 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")


zeallot documentation built on June 8, 2025, 1:36 p.m.