Preface (by Tal Galili)
magrittr: The Difficult Crossing – by Stefan Milton Bache
Background
It has only been 7 months and a bit since my initial magrittr
commit to GitHub on January 1st. It has had more success than I had anticipated, and it appears that I was not quite alone with a frustration which caused me to start the magrittr
project. I am not easily frustrated with R
, but after a few weeks working with F#
at work, I felt it upon returning to R
: I had gotten used to writing code in a different way — all nicely aligned with thought and order of execution. The forward pipe operator |>
was so addictive that being unable to do something similar in R
was more than mildly irritating. Reversing thought, deciphering nested function calls, and making excessive use of temporary variables almost became deal breakers! Surprisingly, I had never really noticed this before, but once I did my returning to R
became a difficult crossing.
An amazing thing about R
is that it is a very flexible language and the problem could be solved. The |>
operator in F#
is indeed very simple: it is defined as let (|>) x f = f x
. However, the usefulness of this simplicity relies heavily on a concept that is not available in R
: partial application. Furthermore, functions in F#
almost always adhere to certain design principles which make the simple definition sufficient. Suppose that f
is a function of two arguments, then in F#
you may apply f
to only the first argument and obtain a new function as the result — a function of the second argument alone. This is partial application, and works with any number of arguments, but application is always from left to right in the argument list. This is why the most important argument (and the one most likely to be a left-hand side object in the pipeline) is almost always the last argument, which in turn makes the simple definition of |>
work. To illustrate, consider the following example:
some_value |> some_function other_value
Here, some_function
is partially applied to other_value
, creating a new function of a single argument, and by the simple definition of |>
, this is applied to some_value
.
It was clear to me that because R
is lacking native partial application and conventions on argument order, no simple solution would be satisfactory, although definitely possible, see e.g. here or here. I wanted to make something that would feel natural in R
, and which would serve the main purpose of improving cognitive performance of those writing the code, and of those reading the code.
It turned out that while I was working on magrittr’s %>%
operator, Hadley Wickham and Romain Francois was implementing a similar %.%
operator in their dplyr
package which they announced on January 17. However, it was not quite as flexible, and we thought that piping functionality was better placed in its own more light-weight package. Hadley joined the magrittr
project, and in dplyr 2.0
the %.%
operator was deprecated — instead%>%
was imported from magrittr
.
Continue reading “Simpler R coding with pipes > the present and future of the magrittr package”