Backpipe Operations

Christopher Brown


The backpipe package provides a single ’backpipe" operator (%<%) that allows the order of operands in a pipe statuement to be inverted. In some situations this promotes more legible, interpretable and debuggable code.

Popular packages magrittr and pipeR do not provide a backward pipe operator. This package files the void by providing a %<% for use with magrittr and %<<% for use with pipeR.

The package also provides the backpipefunction for defining backpipe operators for any forward pipe implementation


CRAN (release)


Github (devel)



mean %<%  1:5   # magrittr
mean %<<% 1:5   # pipeR
mean %<% range %<% 1:5
# Chained with mixed pipes are performed left-to-right
# Although technically possible, this is probably a bad idea.
add(1) %<% 1:5 %>% multiply_by(2)
1:5 %>% add(1) %>% multiply_by(2)  # same

Keyboard Shortcut

The insert_backpipe function can be bound to a keyboard shortcut. We recommend: CTRL + SHIFT + <

This can be done through RStudio’s Tools > Modify Keyboard Shortcuts menu. Future versions of this package may do this automatically.

Common Use Cases

backpipe can be used to:


magrittr, pipeR and other pipes allow the developer to create left-to-right operation flows. However, some code is better expressed with a right-to-left syntax and is more common than one might expect. As an example, consider how shiny has the developer write HTML-producing code.Let’s say you wished to produce the following HTML:

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

HTML is ugly, but works. To generate this code using shiny, you’d write:

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

Yuck!!! This is almost as ugly as the HTML. magrittr or pipeR allows this can to be cleaner:

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

That’s a little better; it produces the same HTML and is much cleaner, but still has a big problems. The code does not match the output. The ordering of the HTML output is outside-in while the code is ordered inside-out. This disconnect reduces clarity and makes debugging difficult.

With the backpipe operator, the same output can be generated with:

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

Notice how that a) the order of the code now matches the output HTML and b) the indentatoin aligns with the hierachal nature of HTML.

While there is no question about the utility of the forward pipe, it does not always promote the most expressive code. The backpipe solves this problem. In fact, writing cleaner shiny code was the impetus for this package. Though it, can be used to clarify code in other common operations. I am looking at you, testthat and assertthat.

Technical Implementation

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