This function applies a function to leaves of a nested list (depth-first) if and only if the
class of the leaf matches one of the names in class
.
leaf_apply(l, f, class = NULL, name = "", sep = ".", strict.lists = FALSE, is.parent = if (strict.lists) function(x, n, ...) { class(x)[1] == "list" } else function(x, n, ...) { is.list(x) && (is.null(class) || !inherits(x, as.character(class))) })
l | the list to be traversed. |
---|---|
f | the function to be applied to the leaves of the list. |
class | a vector of class names (as quoted strings).
All and only objects inheriting from any of
these will be considered a leaf, even if they are also lists.
Any objects that are neither lists nor inherit from |
name | a prefix for the names in the resulting list |
sep | the separator to be used when creating the names of the leaves from the list nesting. |
strict.lists | a logical. If |
is.parent | a function of an item and item name that returns a logical indicating whether a node is a leaf. See the default vaule for examples. |
a flat, named list of the results of applying f
to the leaves of l
.
f
must be of the form f(item, item_name)
where
item
is a member of l
and
item_name
is the concatenated name of item
created
by applying sep
between node names in the list.