[Rd] Additional an example for the forward pipe operator's documentation
Erez Shomron
erez@homron @end|ng |rom myko|@b@com
Sat Jun 19 10:21:55 CEST 2021
Hello,
While playing around with the new forward pipe operator I've noticed
there's a possibly overlooked usage for the operator, which would be
very beneficial to document.
Whenever you want the LHS to be passed to an argument other than the
first, the documented example demonstrates how to do that with an
anonymous function.
However the syntax is less than ideal (aesthetically):
mtcars |> subset(cyl == 4) |> (function(d) lm(mpg ~ disp, data = d))()
Fortunately there's a better, undocumented option using named arguments:
mtcars |> subset(cyl == 4) |> lm(formula = mpg ~ disp)
The reason this works, is because of how R matches arguments. As the
language definition states, first named arguments are matched, then
partial matching, and only afterwards positional arguments are matched.
I think people that are frustrated with former syntax would be happy to
know the latter option exists.
That's just my opinion.
Thank you for reading,
Erez
More information about the R-devel
mailing list