Description Usage Arguments Details Value Examples
Embed a value into a list (possibly named) that has an arbitrary number of levels.
1 |
levels |
Either the desired number of levels or their explicit names. For the
latter, the last element of |
value |
Value to be nested. |
Function nest()
embeds (nests) value
into a tree.
Each node is a list, except for the last one
which is set equal to value
. Nodes are named if non-empty
strings are passed to levels
. See examples.
1 2 3 4 5 6 7 8 9 10 11 | <level 0> (top-level)
│
└── <level 1>
│
└── <level 2>
│
└── <...>
│
└── <terminal node>
│
└── value
|
As an example, calling nest(c("lvl1", "lvl2", "terminal_node"))
yields
the following tree.
1 2 3 4 5 6 7 |
Passing either 0L
or character(0L)
to argument levels
yields
a degenerate case: it implies value
should not be nested into any
list.
A list, possibly named if levels
is a
character
vector, with a number of levels prescribed by levels
.
If levels
is either 0
or character(0L)
, nest()
simply returns
value
. See details.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | ## Nest a value into a list of 0, 1, 2, or 3 levels.
nest(0L, "my nested value")
nest(1L, "my nested value")
nest(2L, "my nested value")
nest(3L, "my nested value")
## Nest a value into a complex tree of 100 levels.
nest(100L, "deeply nested value")
## Nest a NULL value into a tree of named lists.
x <- nest(c("level_1", "level_2", "level_3", "terminal_node"))
y <- list(
level_1 = list(
level_2 = list(
level_3 = list(terminal_node = NULL))))
identical(x, y) # TRUE
## You can nest any R object, even other lists.
nest(3L, value = nest(3L, value = "Hey!"))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.