%>% to emphasise a sequence of actions, rather than the object that the actions are being performed on.
Avoid using the pipe when:
You need to manipulate more than one object at a time. Reserve pipes for a sequence of steps applied to one primary object.
There are meaningful intermediate objects that could be given informative names.
%>% should always have a space before it, and should usually be followed by a new line. After the first step, each line should be indented by two spaces. This structure makes it easier to add new steps (or rearrange existing steps) and harder to overlook a step.
4.3 Long lines
If the arguments to a function don’t all fit on one line, put each argument on its own line and indent:
4.4 Short pipes
A one-step pipe can stay on one line, but unless you plan to expand it later on, you should consider rewriting it to a regular function call.
Sometimes it’s useful to include a short pipe as an argument to a function in a longer pipe. Carefully consider whether the code is more readable with a short inline pipe (which doesn’t require a lookup elsewhere) or if it’s better to move the code outside the pipe and give it an evocative name.
4.5 No arguments
magrittr allows you to omit
() on functions that don’t have arguments. Avoid this feature.
There are three acceptable forms of assignment:
Variable name and assignment on separate lines:
Variable name and assignment on the same line:
Assignment at the end of the pipe with
I think this is the most natural to write, but makes reading a little harder: when the name comes first, it can act as a heading to remind you of the purpose of the pipe.
The magrittr package provides the
%<>% operator as a shortcut for modifying an object in place. Avoid this operator.