Popular packages magrittr and pipeR packages do not provide a backward piping operator. This package provides operators %<% for use with magrittr and %<<% for use with pipeR. The package also provides the backpipefunction for defining backpipe operators

Motivation

Some structure are simply better expressed with right-to-left operations. This more common than one might expect. For example, right-to-left structures are better at representing (some) trees, directories and nested structures.
As an example, consider a directory structure. Typically files on a directory are described by starting at the root node, such as /home/cbrown/docs/file. To programattically assemble this using common pipe, one might code:

"file" %>% file.path('docs') %>% file.path('cbrown') %>% file.path('home')

That is, forward-piping has forced a reversal of the natural ordering. The backpipe operators allows for a more natural and readable syntax:

file.path('home') %<% file.path('cbrown') %<% file.path('docs') %<% "file"

As a second example, consider how shiny has the developer write code that produces HTML.

h1( "content", role="heading" )        %>%
  div( class="inner")                  %>%
    div( class="outer")                %>% 
      div( class="outer-outer")      
  

This produces valid HTML:

<div class="outer-outer">
  <div class="outer">
    <div class="inner">
      <h1 role="heading">content</h1>
    </div>
  </div>
</div> 

but illustrates two incongruities between code and output. The First is the aforemention reversal of order between code and output. This makes it hard to debug the generated HTML. The second problems is that code indentation no longer illustrate the nested structure of the output. In fact, writing cleaner shiny code was the motivation for the creation of the package. Using the backpipe operator the same code can be written as:

div( class="outer-outer")              %<%
  div( class="outer")                  %<% 
    div( class="inner")                %<% 
      h1( "content", role="heading" ) 

This much better matches the generated HTML output.

Usage

The back-piping operators

mean %<%  1:3   # magrittr
mean %<<% 1:3   # pipeR
 
mean %<% range %<% 1:5

Technical Implementation

The backpipe operators are implemented as a simple reording of arguments. See the backpipe code for more details.

References