Our R
package wrapr
supplies a “piping operator” that we feel is a real improvement in R code piped-style coding.
The idea is: with wrapr
‘s “dot arrow” pipe “%.>%
” the expression “A %.>% B
” is treated very much like “{. <- A; B}
”. In particular this lets users think of “A %.>% B(.)
” as a left-to-right way to write “B(A)
” (i.e. under the convention of writing-out the dot arguments, the pipe looks a bit like left to right function composition, call this explicit dot notation).
This sort of notation becomes useful when we compose many steps. Some consider “A %.>% B(.) %.>% C(.) %.>% D(.)
” to be easier to read and easier to maintain than “D(C(B(A)))
”.
In terms of the popular magrittr
pipe: wrapr
dot arrow “A %.>% B
” behaves a lot like “A %>% {B}
” (“{}
” on the right being magrittr
‘s special notation to treat the right-hand side as an expression instead of as a function call). Like magrittr
, wrapr
dot arrow can be used to write “sin(5)
” as “5 %.>% sin
”, though the preferred wrapr
notation is the explicit dot notation: “5 %.>% sin(.)
”. Unlike magrittr
, wrapr
deliberately does not accept “only parenthesis notation” “5 %>% sin()
” (our view is: if the user goes to all the trouble to tell us there are no arguments, wrapr
dot arrow should take their word for it).
wrapr
dot arrow strives to be regular (have few different operating modes), giving the user a lot options and a lot of expressive power. Please see here for a small study we conducted on this.
Lets show a few examples that all share a secret feature that we will reveal at the end of this article (note, any example prior to here is not part of the secret).
First we can pipe into package qualified functions (with or without the explicit dot notation).
1 |
|
1 |
|
In the function notation (without the dot) argument names are preserved.
1 |
|
1 |
|
1 |
|
1 |
|
Also, piping into functions held as list
items is supported.
1 |
|
1 |
|
1 |
|
And piping into parenthesized expressions is supported, so it is safe to add clarifying parenthesis when one wishes.
1 |
|
1 |
|
In the dot notation piping into nested functions is handled smoothly.
1 |
|
1 |
|
wrapr
dot arrow is compatible with dplyr
“pronoun” notation (the “.data
” and “.env
” qualifiers below).
1 |
|
1 |
|
wrapr
dot arrow can even be used inside dplyr
expressions.
1 |
|
1 |
|
Some nested items can be subtle, for in the example below the dot in “rev(.)[2]
” is actually not a top-level argument as the “rev(.)
” expression is technically an argument to the []
operator. Unlike magrittr
, wrapr
does not change its substitution behavior based on the presence or absence of an argument named “.
” at the top-level of expressions, making it easy to reason about expression semantics.
1 |
|
1 |
|
wrapr
dot arrow is compatible with data.table
, even with the common addition of visibility controlling []
-calls.
1 |
|
1 |
|
(Note: if the wrapr
package is attached before data.table
their will be a warning regarding “:=
”. This is actually not a problem as data.table
uses the “:=
” symbol in its own parsing and in its own package context where it can not be confused with wrapr
‘s definition.)
Another subtle form of expression nesting are operators such as %in%
. In expressions such as “filter(d, a %in% .)
”, the dot is not an argument to filter()
but an argument to %in%
.
1 |
|
1 |
|
1 |
|
1 |
|
These are all of our examples. We are now ready to share their common secret.
None of the above examples work when translated into
magrittr
pipelines (unless one adds braces and explicit dot-arguments).
When we were not thinking about it deeply all of the above code likely seemed reasonable. In fact it was reasonable, it just doesn’t happen to follow magrittr
conventions.
We call out just a couple to show the issues.
1 |
|
1 |
|
1 |
|
1 |
|
The above illustrates a problem we run into in promoting wrapr
dot arrow: the misconception that the magrittr
pipe leaves no room for improvement.
Our impression is magrittr
users eventually learn and internalize what variations work and do not work for magrittr
, and then learn to limit their coding to stay in the safe magrittr
fragment. In our opinion once one learns the one limitation of wrapr
dot arrow (the requirement of explicit dot arguments, due to the expression orientation of the pipe) one can use dot arrow as a much more powerful pipe that supports many more coding patterns (such piping into function references, as we showed above).
For those that are interested we have tutorials and formal documentation.
Like this:
Like Loading…
Related