Description Usage Arguments Invariants Examples
Flat map is similar to map, except that the size restriction on the output
of each .f
call is lifted. This means that rather than requiring a result
of size 1
from each function call, a flat map can return an object of
arbitrary size.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | flat_map_vec(.x, .f, ..., .ptype = NULL, .name_spec = NULL,
.name_repair = c("minimal", "unique", "check_unique", "universal"))
flat_map_lst(.x, .f, ..., .name_spec = NULL,
.name_repair = c("minimal", "unique", "check_unique", "universal"))
flat_map_dbl(.x, .f, ..., .name_spec = NULL,
.name_repair = c("minimal", "unique", "check_unique", "universal"))
flat_map_int(.x, .f, ..., .name_spec = NULL,
.name_repair = c("minimal", "unique", "check_unique", "universal"))
flat_map_chr(.x, .f, ..., .name_spec = NULL,
.name_repair = c("minimal", "unique", "check_unique", "universal"))
flat_map_lgl(.x, .f, ..., .name_spec = NULL,
.name_repair = c("minimal", "unique", "check_unique", "universal"))
|
.x |
A list or atomic vector. |
.f |
A function, formula, or vector (not necessarily atomic). If a function, it is used as is. If a formula, e.g.
This syntax allows you to create very compact anonymous functions. If character vector, numeric vector, or list, it is
converted to an extractor function. Character vectors index by
name and numeric vectors index by position; use a list to index
by position and name at different levels. If a component is not
present, the value of |
... |
Vectors to coerce. |
.ptype |
If Alternatively, you can supply |
.name_spec |
A name specification for combining
inner and outer names. This is relevant for inputs passed with a
name, when these inputs are themselves named, like
See the name specification topic. |
.name_repair |
How to repair names, see |
vec_size(flat_map_vec(.x, .f)) == sum(map_int(map(.x, .f), vec_size))
vec_ptype(flat_map_vec(.x, .ptype = ptype)) == ptype %||% vec_ptype_common(!!! map(.x, .f))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | # `map()` functions require that each result from `.f` have size 1
try(map_dbl(list(1, 2:3, 4:6), ~.x))
# `flat_map_dbl()` and friends don't care
flat_map_dbl(list(1, 2:3, 4:6), ~.x)
# Returning arbitrary output types
flat_map_vec(1:3, ~Sys.Date() + seq(0, .x))
# Returning data frames of arbitrary sizes
flat_map_vec(1:3, ~data.frame(x = seq_len(.x)))
# Name repair if required
flat_map_dbl(list(x = 1, x = 2), ~c(a = .x), .name_spec = "{outer}_{inner}")
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.