
Last chance! 50% off unlimited learning
Sale ends in
map_safely()
, map_quietly()
and map_peacefully()
are variants of
purrr::map()
that wrap the supplied function .f
using purrr::safely()
and/or purrr::quietly()
in order to capture various side effects. Lists
mapped in this way have an associated class added to them, allowing them to
succinctly summarise captured side effects when displayed in a tibble.
map_safely(.x, .f, otherwise = NULL, quiet = TRUE, ...)map_quietly(.x, .f, ...)
map_peacefully(.x, .f, ...)
map2_safely(.x, .y, .f, otherwise = NULL, quiet = TRUE, ...)
map2_quietly(.x, .y, .f, ...)
map2_peacefully(.x, .y, .f, ...)
pmap_safely(.l, .f, otherwise = NULL, quiet = TRUE, ...)
pmap_quietly(.l, .f, ...)
pmap_peacefully(.l, .f, ...)
future_map_safely(.x, .f, otherwise = NULL, quiet = TRUE, ...)
future_map_quietly(.x, .f, ...)
future_map_peacefully(.x, .f, ...)
future_map2_safely(.x, .y, .f, otherwise = NULL, quiet = TRUE, ...)
future_map2_quietly(.x, .y, .f, ...)
future_map2_peacefully(.x, .y, .f, ...)
future_pmap_safely(.l, .f, otherwise = NULL, quiet = TRUE, ...)
future_pmap_quietly(.l, .f, ...)
future_pmap_peacefully(.l, .f, ...)
A list or atomic vector.
A function, formula or atomic vector, as specified by
purrr::as_mapper()
.
Default value to use when an error occurs.
Hide errors (TRUE
, the default), or display them as they
occur?
Other arguments supplied to purrr::map()
or its variants, or to
furrr::future_map()
or its variants..
A list or atomic vector, of the same length as .x
.
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.
A list of the same length as .x
. Each element of the returned list
is itself a named list, structured according to the captured side effects.
The Details section elaborates on these side effects.
map_safely()
will summarise the returned list with a fixed-width
string of two (spaced) columns:
If a result
component is present, R
appears, and
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:
If a result
component is present, R
appears,
If an output
component is present, O
appears,
If a messages
component is present, M
appears, and
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:
map2_safely()
map2_quietly()
pmap_safely()
pmap_quietly()
Further variants, prefixed by future_
, allow safe or quiet mapping to
happen in parallel if you have the furrr
package installed:
future_map_safely()
future_map_quietly()
future_map2_safely()
future_map2_quietly()
future_pmap_safely()
future_pmap_quietly()
# NOT RUN {
library(tibble)
library(dplyr)
library(tidyr)
library(collateral)
# like map(), these can be used to iterate over vectors or lists
list("a", 10, 100) %>% map_safely(log)
list(5, -12, 103) %>% map_quietly(log)
# if you're using tibbles, you can also iterate over list-columns,
# such as nested data frames
mtcars %>%
rownames_to_column(var = "car") %>%
as_tibble() %>%
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(data = -cyl) %>%
mutate(qlog = map_quietly(data, ~ log(.$wt)))
# }
Run the code above in your browser using DataLab