flat-map: Flat map

Description Usage Arguments Invariants Examples

Description

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.

Usage

 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"))

Arguments

.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. ~ .x + 2, it is converted to a function. There are three ways to refer to the arguments:

  • For a single argument function, use .

  • For a two argument function, use .x and .y

  • For more arguments, use ..1, ..2, ..3 etc

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 .default will be returned.

...

Vectors to coerce.

.ptype

If NULL, the default, the output type is determined by computing the common type across all elements of ....

Alternatively, you can supply .ptype to give the output known type. If getOption("vctrs.no_guessing") is TRUE you must supply this value: this is a convenient way to make production code demand fixed types.

.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 outer = c(inner = 1), or when they have length greater than 1: outer = 1:2. By default, these cases trigger an error. You can resolve the error by providing a specification that describes how to combine the names or the indices of the inner vector with the name of the input. This specification can be:

  • A function of two arguments. The outer name is passed as a string to the first argument, and the inner names or positions are passed as second argument.

  • An anonymous function as a purrr-style formula.

  • A glue specification of the form "{outer}_{inner}".

See the name specification topic.

.name_repair

How to repair names, see repair options in vec_as_names().

Invariants

Examples

 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}")

DavisVaughan/flatcat documentation built on Oct. 30, 2019, 5 p.m.