Gabriel (de Selding?) has a tutorial on how to use the various pipes in R:
In F#, the pipe-forward operator
|>
is syntactic sugar for chained method calls. Or, stated more simply, it lets you pass an intermediate result onto the next function.Remember that “chaining” means that you invoke multiple method calls. As each method returns an object, you can actually allow the calls to be chained together in a single statement, without needing variables to store the intermediate results.
In R, the pipe operator is, as you have already seen,
%>%
. If you’re not familiar with F#, you can think of this operator as being similar to the+
in aggplot2
statement. Its function is very similar to that one that you have seen of the F# operator: it takes the output of one statement and makes it the input of the next statement. When describing it, you can think of it as a “THEN”.
Auto-recommended for the F# love, and a good tutorial to boot.
John Mount has a few interesting notes on the topic:
-
data.table has essentially used the square bracket sequence “
][
” in a manner equivalent to piping in R since about 2006. Here is an example. -
The Bizarro Pipe “
->.;
” has always been possible in R, though I worked it out and started teaching it in 2016. And it is as quick to type as any other notation once you bind it to a keyboard shortcut.
Read on for the rest of his notes, too.