Skip to content

Pipe precedence

5 messages · Caitlin Gibbons, Eric Berger, Jeff Newmiller +1 more

#
What is the precedence of the new |> pipe operator? I don't see it mentioned in ?Syntax, nor does it come up when I search the R Language Definition document.
#
I didn?t know R had a new pipe operator, but I have seen ?|>? (without quotes) used in the Elixir language though. Are there now two? ?%>%? from the magrittr package and ?|>? which is built-in?
#
This is part of the R-4.1.0 release which came out a few days ago.
See
1. https://stat.ethz.ch/pipermail/r-announce/2021/000670.html
2.
https://www.jumpingrivers.com/blog/new-features-r410-pipe-anonymous-functions/
3.
https://community.rstudio.com/t/psa-r-4-1-0-release-requires-rstudio-preview/105209

Eric


On Sun, May 23, 2021 at 6:07 AM Caitlin Gibbons <bioprogrammer at gmail.com>
wrote:

  
  
#
R 4.1.0 was released this week with a new pipe operator and a new anonymous function shorthand (\(x) x^2).

The pipe operator is not quite as flexible as the magrittr pipe, but it is faster (not that the magrittr pipe is noticably slow) and built-in.
On May 22, 2021 8:07:27 PM PDT, Caitlin Gibbons <bioprogrammer at gmail.com> wrote:

  
    
#
On 22/05/2021 8:26 p.m., Jeff Newmiller wrote:
It's the same precedence as the %any% operators listed in the ?Syntax 
table, so it matches the magrittr pipe priority.  You can see this in 
the source if you know how to read Bison, or determine it experimentally:

 > 2 * 3 |> print()
[1] 3
[1] 6

This is the same result as

2 * ( 3 |> print() )

so it has higher priority than * .

On the other hand,

 > 2 : 3 |> print()
[1] 2 3

is the same as

( 2 : 3 ) |> print()

so it has lower priority than : .

A difference with the magrittr operator is in debugging.  If an error 
happens in the middle of a pipe, I find the traceback a little easier to 
understand with the built-in pipe:

 > 2 |> print() |> stop() |> mean()
[1] 2
Error in mean(stop(print(2))) : 2
 > traceback()
2: stop(print(2))
1: mean(stop(print(2)))

versus

 > 2 %>% print() %>% stop() %>% mean()
[1] 2
Error in mean(.) : 2
 > traceback()
3: stop(.)
2: mean(.)
1: 2 %>% print() %>% stop() %>% mean()

Duncan Murdoch