R 4.1.0 famously introduced the |>
("base pipe") operator and Haskell-like lambda function syntax.
I thought it would be possible to combine the two like this:
c(1, 2, 3) |> \(x) 2 * x
This fails for me with:
Error: function 'function' not supported in RHS call of a pipe
I thus assume this is not valid syntax? This works:
c(1, 2, 3) |> (\(x) 2 * x)()
Is there a more elegant way to chain the pipe and the new lambda functions?
I think the most elegant way is with curly braces:
c(1, 2, 3) |> {\(x) 2 * x}()
but this works too:
c(1, 2, 3) |> (\(x) 2 * x)()
The latter is recommended in ?`|>`
:
# or use an anonymous function:
mtcars |> subset(cyl == 4) |> (function(d) lm(mpg ~ disp, data = d))()
mtcars |> subset(cyl == 4) |> (\(d) lm(mpg ~ disp, data = d))()