map_safely and map_quietly are variants of map that:

  1. wrap the supplied function .f with either safely or quietly, and

  2. add a class to the returned output list in order to format it nicely when it (or a tibble it appears in) is printed.

map_safely(.x, .f, ...)

map_quietly(.x, .f, ...)

map2_safely(.x, .y, .f, ...)

map2_quietly(.x, .y, .f, ...)

pmap_safely(.l, .f, ...)

pmap_quietly(.l, .f, ...)

Arguments

.x

A list or atomic vector.

.f

A function, formula or atomic vector.

...

Other arguments supplied to map.

.y

A list or atomic vector, of the same length as .x.

.l

A list of lists. The length of .l determines the number of arguments that .f will be called with. List names will be used if present.

Value

A list of the same length as .x. The list elements contain results and captured side effects as described in safely and quietly.

Details

map_safely will summarise the returned list with a fixed-width string of two (spaced) columns:

  1. If a result component is present, R appears, and

  2. If an error component is present, E appears.

If either component is missing, an underscore (_) appears in its place.

Similarly, map_quietly will summarise the returned list with a fixed-width string of four (spaced) columns:

  1. If a result component is present, R appears,

  2. If an output component is present, O appears,

  3. If a messages component is present, M appears, and

  4. If a warnings component is present, W appears.

If any is missing, an underscore (_) appears in its place.

Variants for iterating over two or more inputs simultaneously are also provided and function identically to their purrr counterparts:

  1. map2_safely

  2. map2_quietly

  3. pmap_safely

  4. pmap_quietly

Examples

library(magrittr) # like map(), these can be used to iterate over vectors or lists list("a", 10, 100) %>% map_safely(log)
#> _ E #> R _ #> R _
list(5, -12, 103) %>% map_quietly(log)
#> R O _ _ #> R O _ W #> R O _ _
suppressMessages(library(tidyverse))
#> Warning: package 'ggplot2' was built under R version 3.5.1
#> Warning: package 'dplyr' was built under R version 3.5.1
# if you're using tibbles, you can also iterate over list-columns, # such as nested data frames mtcars %>% rownames_to_column(var = "car") %>% as_data_frame() %>% select(car, cyl, disp, wt) %>% # spike some rows in cyl == 4 to make them fail mutate(wt = dplyr::case_when( wt < 2 ~ -wt, TRUE ~ wt)) %>% # nest and do some operations quietly() nest(-cyl) %>% mutate(qlog = map_quietly(data, ~ log(.$wt)))
#> # A tibble: 3 x 3 #> cyl data qlog #> <dbl> <list> <collat> #> 1 6 <tibble [7 x 3]> R O _ _ #> 2 4 <tibble [11 x 3]> R O _ W #> 3 8 <tibble [14 x 3]> R O _ _