Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

magrittr-package

magrittr - Ceci n'est pas un pipe


Description

The magrittr package offers a set of operators which promote semantics that will improve your code by

  • structuring sequences of data operations left-to-right (as opposed to from the inside and out),

  • avoiding nested function calls,

  • minimizing the need for local variables and function definitions, and

  • making it easy to add steps anywhere in the sequence of operations.

The operators pipe their left-hand side values forward into expressions that appear on the right-hand side, i.e. one can replace f(x) with x %>% f, where %>% is the (main) pipe-operator.

Details

Consider the example below. Four operations are performed to arrive at the desired data set, and they are written in a natural order: the same as the order of execution. Also, no temporary variables are needed. If yet another operation is required, it is straight-forward to add to the sequence of operations whereever it may be needed.

For a more detailed introduction see the vignette (vignette("magrittr")) or the documentation pages for the available operators:

%>% pipe.
%T>% tee pipe.
%<>% assignment pipe.
%$% exposition pipe.

Author(s)

Maintainer: Lionel Henry lionel@rstudio.com

Authors:

Other contributors:

  • RStudio [copyright holder, funder]

See Also

Useful links:

Examples

## Not run: 

the_data <-
  read.csv('/path/to/data/file.csv') %>%
  subset(variable_a > x) %>%
  transform(variable_c = variable_a/variable_b) %>%
  head(100)

## End(Not run)

magrittr

A Forward-Pipe Operator for R

v2.0.1
MIT + file LICENSE
Authors
Stefan Milton Bache [aut, cph] (Original author and creator of magrittr), Hadley Wickham [aut], Lionel Henry [cre], RStudio [cph, fnd]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.