How can I use the Map
function with the pipe base |>
?
The next x
vector can be used inside the Map
function
x <- c(1,5,1,2)
Map(function(n)n*2, x) |> unlist()
# [1] 2 10 2 4
This works, but when I try to use the pipe I get the next error:
x |> Map(function(n)n*2, ...= _)
#Error in (function (n) : unused argument (... = dots[[1]][[1]])
So I make another anonymous function, but it is difficult to read
x |> (function(x) Map(function(n)n*2,x))() |> unlist()
#[1] 2 10 2 4
I would like to know why the Map
can't take the place holder _
.
Another solution
I change the order of the arguments of Map
so It can be more legible
myMap <- \(...,f) Map(f, ...)
x |> myMap(f = function(n)n*2) |> unlist()
#[1] 2 10 2 4