Site icon R-bloggers

My Opinion on R’s Upcoming Pipe

[This article was first published on R – Win Vector LLC, 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.

R‘s upcoming pipe appears to be currently proposed as a syntactic transform of the form:

  a |> f(...)   ->    f(a, ...)
  a |> f()      ->    f(a)

There is a current active discussion on this prototype and some interesting points come up. Note the current proposal appears to disallow a |> f -> f(a), a currently popular transform.

  1. This is a language feature presented as a soon-to-be-user-visible prototype, not an RFC.
  2. Some are objecting to the term “pipe.”
  3. Some call this sort of pipe function composition.
  4. It is noticed that this sort of substitution is generally thought of as a “macro.”
  5. There is a claim the proposed pipe seems to violate the beta-reduction rule of the lambda calculus: variables should be substitutable for values. The idea is if the following code fragment is allowed.
      f <- function(x) { x + 1 }
      2 |> f()
    

    Then replacing f with its value should also be valid. And one might even want a strong substitution of expressions, and be able to write:

      2 |> function(x) { x + 1 }
    

    It appears the current R-language |> operator does not allow the second expression unless extra parenthesis are introduced (either to group the function declaration terms or add on an argument evaluation slot). I haven’t tried this, so I may be wrong, I am attempt to excerpt this from the dev email chain.

My comments are as follows.

  1. Point 1 seems a bit too pragmatic for core language features.
  2. The word “pipe” is used in many languages to mean something other than the Unix pipe. It is lore that “Unix pipe is the only pipe” bullying is why R’s magrittr package, the supplier of the popular R-extension pipe, is named magrittr.
  3. There is a strong analogy to function composition, but there are some details that relate this strongly to function application or even macro application.
  4. Macros or marcro-like entities in R are likely a bigger problem than one might expect. What R calls “functions” are closures (they capture environments) with essentially Lisp FEXPR semantics. These forms were largely abandoned in later Lisps in favor of a split into functions that work in applicative order (arguments are evaluated before the function is evaluated) and macros (roughly transformations on code). According to the Wikipedia, Kent Pitman argued in 1980 that once you have macros, FEXPRs become hard to defend.
  5. Point 5 probably doesn’t matter to the end users. The popular R pipe magrittr doesn’t allow this form either. Also, such an objection may be confusing substitution of expressions with substitution of values. One doesn’t expect to substitute the expression side of x <- 1 + 2 into x * 3 without some extra parenthesis. However, I feel there are likely some important points of this form that have not been discussed in a large enough venue at this time. We may all be missing something if we don’t listen to feed back such as this.
  6. A lot of the issue is: R FEXPRs get their arguments un-evaluated. One can use this to implement a lot of language features (control structures, domain specific mini-languages) at the user or package level. R packages really do feel like R extensions. However, FEXPRs don’t receive their arguments un-parsed, so some things are not possible at this level.
  7. One of the objections to package supplied pipes is the requirement of verbose user-specified infix operators that start and end with “%“. This is why magrittr’s pipe is written as “%>%” (there are also some issues of operator precedence, but they are minor and fixed by the occasional introduction of parenthesis). data.table essentially uses “][” as a pipe operator without needing any syntactic hooks, instead relying on the self-return conventions of “[]“.

My conclusions/opinions are the following.

And those are my very unsolicited thoughts on the new R pipe. I admit: I have dog in the race, my own pipe that I use in my own work. I feel in addition to possibly coloring my opinions (which I am trying to be careful about) that also gives me some relevant experience. I’ve already inserted one message into the R-dev email chain, so I am trying to limit my comments to this blog which is less of an imposition on R-dev subscribers.

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

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.