[Rd] Generalised piping into operators
Michael Milton
ttm|gue|tt @end|ng |rom gm@||@com
Sat Apr 22 10:42:27 CEST 2023
This is all very intriguing. Normally when piping into an operator
function, R throws "Error: function '+' not supported in RHS call of a
pipe":
> 1 |> `+`(2)
Error: function '+' not supported in RHS call of a pipe
This is a different error from the above "invalid use of pipe placeholder",
which makes me think it was added for a different purpose than prohibiting
operator precedence issues. I am interested in the justification of this
error.
However, as you have noted, when using the placeholder, R has no issue with
this syntax (which I didn't realise!):
> 1 |> `+`(x=_, 2)
[1] 3
On Sat, Apr 22, 2023 at 3:00 AM Duncan Murdoch <murdoch.duncan using gmail.com>
wrote:
> On 21/04/2023 12:16 p.m., Michael Milton wrote:
> > I'm afraid I don't understand. I know that parsing `+`(1, 1) returns a
> > result equivalent to `1 + 1`, but why does that impose a restriction on
> > parsing the pipe operator? What is the downside of allowing arbitrary
> > RHS functions?
>
> I thought the decision to exclude "_ + 1" happens after enough parsing
> has happened so that the code making the decision can't tell the
> difference between "_ + 1" and "`+`(_, 1)". I might be wrong about
> that, but this suggests it:
>
> > quote(_ + 1)
> Error in quote("_" + 1) : invalid use of pipe placeholder (<input>:1:0)
> > quote(`+`(_, 1))
> Error in quote("_" + 1) : invalid use of pipe placeholder (<input>:1:0)
>
> On the other hand, this works:
>
> > quote(x |> `+`(e1 = _, 1))
> x + 1
>
> So maybe `+`() is fine after all.
>
> Duncan Murdoch
>
>
[[alternative HTML version deleted]]
More information about the R-devel
mailing list