Playing With Pipe Notations

[This article was first published on R – Win-Vector Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Recently Hadley Wickham prescribed pronouncing the magrittr pipe as “then” and using right-assignment as follows:

NewImage

I am not sure if it is a good or bad idea. But let’s play with it a bit, and perhaps readers can submit their experience and opinions in the comments section.

Right assignment

Right assignment is a bit of an oddity in programming languages. Offhand I can think of a few programming languages that use it: COBOL, TI-Basic, and Forth (due to its value-stack notation).

I have written a bit about right assignment in R in the past. And that led to some interesting discussion.

Frankly I thought right assignment was prohibited in Hadley Wickham’s own style guide. But as Gabe Becker taught me a while ago: there isn’t actually any right-arrow in R (so maybe “Use <-, not =, for assignment.” allows ->).

substitute(5 -> x)
# x <- 5

Another point: R pipes are very closely related to right assignment notation, so once you allow right assignment you don’t actually need pipes in the current R sense (though other forms of pipes such as Unix pipes would be a great addition).

“then”

The idea of having a canonical “pronunciation” for symbols is not a new one. It is fairly standard practice in the Unix community (one reference here). The McIlroy Unix pipe|” (which streams partial results, resulting in very powerful concurrent composition) is said to be read as “pipe”, “pipe to”, “to”, “thru” (and a few more variations). In this era of keyboard shortcuts it is worth considering more verbose piping operators.

Let’s try the idea.

library("dplyr")
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

d <- data.frame(x = 1:3)


`%then%` <- magrittr::`%>%`

d %then%
  mutate(., y = x + 1) %then%
  knitr::kable(.)
#> Error in pipes[[i]]: subscript out of bounds

I’d say this fails on at least two counts, the first “%then%” doesn’t seem grammatical (as d is a noun), and magrittr pipes can’t be associated with a new name (as they are implemented by looking for theirselves by name in captured unevaluated code).

However, the wrapr dot arrow pipe can take on new names.

Let’s try a variation, using a traditional pronunciation: “to”.

`%to%` <- wrapr::`%.>%`

d %to%
  mutate(., y = x + 1) %to%
  knitr::kable(.)
x y
1 2
2 3
3 4

Conclusion

I am still not sure about the above notation one way or the other. Notational prescriptions are at best proposals or “requests for comment”, and need to consider context and precedent to be useful.

To leave a comment for the author, please follow the link and comment on their blog: R – Win-Vector Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)