listify: Create a List from an Object

listifyR Documentation

Create a List from an Object

Description

Coerce an object to a list.

Usage

listify(x)

Arguments

x

an object.

Details

listify is mostly used in combination with mapply or .mapply. For the dots argument of .mapply, each element of which is looped over, listify is a convenient way to turn each element into an iterable version of itself.

Value

A list.

Note

listify has unusual behaviour for data.frame objects.

An S4 data.frame (created by x <- new("data.frame")) will return x. This is because inherits(x, "list") for an S4 object x uses extends(class(x), "list") to determine if x inherits from "list" (which it does for and S4 data.frame).

An unmodified S3 data.frame (created by x <- data.frame()) will return list(x). This is because inherits(x, "list") for an S3 object x with a class attribute uses "list" %in% oldClass(x) to determine if x inherits from "list" (which it does not in this case).

An S3 data.frame could be modified (such as x <- structure(data.frame(), class = c("data.frame", "list"))) such that listify would return x. Where "list" %in% oldClass(x) was FALSE before, it is TRUE here.

Examples

listify(5)        # when 'x' is not a list, returns 'list(x)'
listify(list(5))  # when 'x' is a list, returns 'x'


## when 'x' is an S4 data.frame, returns 'x'
listify(methods::new("data.frame"))


## when 'x' is an unmodified S3 data.frame, returns 'list(x)'
listify(data.frame())


## S3 data.frame 'x' could be modified such that 'listify' returns 'x'
listify(structure(data.frame(), class = c("data.frame", "list")))

ArcadeAntics/essentials documentation built on Nov. 7, 2024, 4:33 p.m.