Skip to contents

Map over leaves of a nested list

Usage

list_map(.x, .f, ..., .progress = FALSE)

Arguments

.x

A nested list to traverse.

Lists are traversed recursively. Non-list objects are treated as leaves. Data frames are not traversed and are treated as leaf values.

.f

A function, formula, or function-like object coercible by rlang::as_function(). Additional arguments supplied through ... are passed on to .f.

...

Additional arguments passed to .f.

.progress

Whether to show a progress

Use TRUE to turn on a basic progress, use a string to give it a name, or use a list to customize the progress bar (see ?cli::cli_progress_bar and https://cli.r-lib.org/articles/progress-advanced.html#cli_progress_bar for details).

Value

A list with the same nested structure as .x, where each leaf has been replaced by the result of calling .f on that leaf.

Examples

# \donttest{
x <- list(
  a = 1,
  b = list(
    x = 1,
    y = list(i = 1, j = 2)
  )
)
x2 <- list_map(x, function(x) x - 2)
# $a
# [1] -1
#
# $b
# $b$x
# [1] -1
#
# $b$y
# $b$y$i
# [1] -1
#
# $b$y$j
# [1] 0
# }