chuck: Get an element deep within a nested data structure, failing...

View source: R/pluck.R

chuckR Documentation

Get an element deep within a nested data structure, failing if it doesn't exist

Description

chuck() implements a generalised form of [[ that allow you to index deeply and flexibly into data structures. If the index you are trying to access does not exist (or is NULL), it will throw (i.e. chuck) an error.

Usage

chuck(.x, ...)

Arguments

.x

A vector or environment

...

A list of accessors for indexing into the object. Can be an positive integer, a negative integer (to index from the right), a string (to index into names), or an accessor function (except for the assignment variants which only support names and positions). If the object being indexed is an S4 object, accessing it by name will return the corresponding slot.

Dynamic dots are supported. In particular, if your accessors are stored in a list, you can splice that in with ⁠!!!⁠.

See Also

pluck() for a quiet equivalent.

Examples

x <- list(a = 1, b = 2)

# When indexing an element that doesn't exist `[[` sometimes returns NULL:
x[["y"]]
# and sometimes errors:
try(x[[3]])

# chuck() consistently errors:
try(chuck(x, "y"))
try(chuck(x, 3))

purrr documentation built on Aug. 10, 2023, 9:08 a.m.