Automatically map complex operations safely
or quietly
, and quickly see the captured side effects.
The collateral
package extends the power of purrr::safely()
and purrr::quietly()
, providing variants of map()
—map_safely()
and map_quietly()
—that automatically apply the appropriate function wrapper and print the resulting output nicely.
This is especially useful for tidy workflows: you can nest()
a data frame, apply a complex operation (such as building a regression model or building and saving a plot) to each group and quickly see which groups need to be inspected in more detail:
We’ll submit to CRAN soon, but for now, use devtools
to install collateral
:
library(tidyverse)
library(collateral)
test =
# tidy up and trim down for the example
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)))
test
#> # A tibble: 3 x 4
#> 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 _ _
collateral
outputcollateral
uses pillar
to style output, so supported terminals will also color the output! Properly styling knitted output is still on the to-do list, though.
If you’re a fan of iterating over two or more inputs in lockstep, then map2()
and pmap()
variants are also provided:
map2_safely()
,map2_quietly()
,pmap_safely()
, andpmap_quietly()
.collateral
also provides tally_*()
and has_()
functions to help you filter or summarise your side effects, as well as a summary()
mehod. If you haven’t used a list-column workflow before, take a look at the vignette to see how it’s done!
If you have a problem with collateral
, please don’t hesitate to file an issue or get in touch with me!