[R] Pipe operator

Ivan Calandra |v@n@c@|@ndr@ @end|ng |rom |e|z@@de
Wed Jan 4 08:20:50 CET 2023


Maybe I missed it in the whole discussion, but since R 4.2.0 the base R 
pipe operator also has a placeholder '_' to specify where the result of 
the left-hand side should be used in the right-hand side (see 
https://stat.ethz.ch/pipermail/r-announce/2022/000683.html).

So the only difference in usage between >%> and |> is that the 
placeholder '.' of the magrittr pipe can appear several times.
It would also be nice if R/Rstudio had a default keyboard shortcut to 
insert the base R pipe like for the magrittr pipe (Ctrl+Shift+M or 
Cmd+Shift+M). The vertical bar is not always easy to find (especially 
when you switch between Mac, Windows and different languages).

Ivan


On 03/01/2023 19:34, avi.e.gross using gmail.com wrote:

> Tim,
>
> There are differences and this one can be huge.
>
> The other pipe operators let you pass the current object to a later argument
> instead of the first by using a period to represent where to put it. The new
> one has a harder albeit flexible method by creating an anonymous function.
>
> -----Original Message-----
> From: R-help <r-help-bounces using r-project.org> On Behalf Of Ebert,Timothy Aaron
> Sent: Tuesday, January 3, 2023 12:08 PM
> To: Sorkin, John <jsorkin using som.umaryland.edu>; 'R-help Mailing List'
> <r-help using r-project.org>
> Subject: Re: [R] Pipe operator
>
> The pipe shortens code and results in fewer variables because you do not
> have to save intermediate steps. Once you get used to the idea it is useful.
> Note that there is also the |> pipe that is part of base R. As far as I know
> it does the same thing as %>%, or at my level of programing I have not
> encountered a difference.
>
> Tim
>
> -----Original Message-----
> From: R-help <r-help-bounces using r-project.org> On Behalf Of Sorkin, John
> Sent: Tuesday, January 3, 2023 11:49 AM
> To: 'R-help Mailing List' <r-help using r-project.org>
> Subject: [R] Pipe operator
>
> [External Email]
>
> I am trying to understand the reason for existence of the pipe operator,
> %>%, and when one should use it. It is my understanding that the operator
> sends the file to the left of the operator to the function immediately to
> the right of the operator:
>
> c(1:10) %>% mean results in a value of 5.5 which is exactly the same as the
> result one obtains using the mean function directly, viz. mean(c(1:10)).
> What is the reason for having two syntactically different but semantically
> identical ways to call a function? Is one more efficient than the other?
> Does one use less memory than the other?
>
> P.S. Please forgive what might seem to be a question with an obvious answer.
> I am a programmer dinosaur. I have been programming for more than 50 years.
> When I started programming in the 1960s the only pipe one spoke about was a
> bong.
>
> John
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.
> ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7Ctebert%40ufl.edu%7C73edce5d4
> e084253a39008daedaa653f%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C6380836
> 13362415015%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJB
> TiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=fV9Ca3OAleDX%2BwuPJIONYStrA
> daQhXTsq61jh2pLtDY%3D&reserved=0
> PLEASE do read the posting guide
> https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-proje
> ct.org%2Fposting-guide.html&data=05%7C01%7Ctebert%40ufl.edu%7C73edce5d4e0842
> 53a39008daedaa653f%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638083613362
> 415015%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6I
> k1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=YUnV9kE1RcbB3BwM5gKwKwc3qNKhIVNF
> txOxKmpbGrQ%3D&reserved=0
> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list