I am wanting to learn more about the purrr package. I can see some nicities in conssistency but am struggling with flexibility for arbitrary problems. I have always loved the flexibility and relative ease of use of Map
. I have read about pmap
but it seems more awkward to work with than Map
for a situation where you want to pass an object to the next part and have n lists loop over that object in concert with one another.
Below is a minimal example. The code passes mtcars
on to a loop (Map
in this case) that in turn iterates through writing functions and file extensions and uses the mtcars
from the prior chain to write out a file. How could I write this code using a purrr/tidyverse approach?
I appreciate that I could simply pass c('fst', 'csv')
and build the extension and writing functions from a single vector using match.call
. This MWE is meant to show the need to pass 2 or more lists/vectors over an object being passed along a chain (this obect is in a sense static).
library(tidyverse)
library(fst)
mtcars %>%
{Map(function(fun, ext) {
fun(., sprintf('mtcars.%s', ext))
},
list(fst::write_fst, readr::write_csv),
list('fst', 'csv')
)} %>%
invisible()