pak::pak("cran/purrr@1.0.0")
Mapping
purrr 1.1.0
map_vec()
.
Install purrr 1.0.0 with:
Load the package with:
Mapping
There are three big new mapping features in purrr 1.0.0:
- Progress bars!
- Better errors
- A new
map_*
family member:map_vec()
.
Progress bars
See a progress bar for long running jobs using .progress = TRUE
:
■■■■■■■ 21% | ETA: 8s
■■■■■■■■■■■■■■■■ 50% | ETA: 5s
■■■■■■■■■■■■■■■■■■■■■■■■■ 79% | ETA: 2s
Set .progress
to a string if you want to identify the progress bar (in this case, .progress = "Saving plots"
).
Better errors
map()
and friends now tell you which element caused the problem in the function you mapped.
In this case, we have a list with two numeric and one character value. When we try to divide it by 2 using map()
, we get an error telling us there’s an issue with index 3 ("a"
).
map_vec()
The map_*
family applies a function to each element of a list. We’ve had map()
, map_lgl()
, map_int()
, map_dbl()
, and map_chr()
.
1:3 |> map(\(x) x / 2) # map always returns a list
[[1]]
[1] 0.5
[[2]]
[1] 1
[[3]]
[1] 1.5
Now we have: map_vec()
!
map_vec()
is a generalized map_*()
that works with an arbitrary types of vectors, like dates, factors, and date-times.
It will error if you try to combine different types:
Error in `map_vec()`:
! Can't combine `<list>[[1]]` <character> and `<list>[[2]]` <double>.