I have seen the use of `%>%`

(percent greater than percent) function in some packages like **dplyr** and **rvest**. What does it mean? Is it a way to write closure blocks in R?

## Solution

## %…% operators

`%>%`

has no builtin meaning but the user (or a package) is free to define operators of the form `%whatever%`

in any way they like. For example, this function will return a string consisting of its left argument followed by a comma and space and then it’s right argument.

```
"%,%" <- function(x, y) paste0(x, ", ", y)
# test run
"Hello" %,% "World"
## [1] "Hello, World"
```

The base of R provides `%*%`

(matrix mulitiplication), `%/%`

(integer division), `%in%`

(is lhs a component of the rhs?), `%o%`

(outer product) and `%x%`

(kronecker product). It is not clear whether `%%`

falls in this category or not but it represents modulo.

**expm** The R package, expm, defines a matrix power operator `%^%`

. For an example see Matrix power in R .

**operators** The operators R package has defined a large number of such operators such as `%!in%`

(for not `%in%`

). See http://cran.r-project.org/web/packages/operators/operators.pdf

**igraph** This package defines %–% , %->% and %<-% to select edges.

**lubridate** This package defines %m+% and %m-% to add and subtract months and %–% to define an interval. igraph also defines %–% .

## Pipes

**magrittr** In the case of `%>%`

the magrittr R package has defined it as discussed in the magrittr vignette. See http://cran.r-project.org/web/packages/magrittr/vignettes/magrittr.html

magittr has also defined a number of other such operators too. See the Additional Pipe Operators section of the prior link which discusses `%T>%`

, `%<>%`

and `%$%`

and http://cran.r-project.org/web/packages/magrittr/magrittr.pdf for even more details.

**dplyr** The dplyr R package used to define a `%.%`

operator which is similar; however, it has been deprecated and dplyr now recommends that users use `%>%`

which dplyr imports from magrittr and makes available to the dplyr user. As David Arenburg has mentioned in the comments this SO question discusses the differences between it and magrittr’s `%>%`

: Differences between %.% (dplyr) and %>% (magrittr)

**pipeR** The R package, pipeR, defines a `%>>%`

operator that is similar to magrittr’s %>% and can be used as an alternative to it. See http://renkun.me/pipeR-tutorial/

The pipeR package also has defined a number of other such operators too. See: http://cran.r-project.org/web/packages/pipeR/pipeR.pdf

**postlogic** The postlogic package defined `%if%`

and `%unless%`

operators.

**wrapr** The R package, wrapr, defines a dot pipe `%.>%`

that is an explicit version of `%>%`

in that it does not do implicit insertion of arguments but only substitutes explicit uses of dot on the right hand side. This can be considered as another alternative to `%>%`

. See https://winvector.github.io/wrapr/articles/dot_pipe.html

**Bizarro pipe**. This is not really a pipe but rather some clever base syntax to work in a way similar to pipes without actually using pipes. It is discussed in http://www.win-vector.com/blog/2017/01/using-the-bizarro-pipe-to-debug-magrittr-pipelines-in-r/ The idea is that instead of writing:

```
1:8 %>% sum %>% sqrt
## [1] 6
```

one writes the following. In this case we explicitly use dot rather than eliding the dot argument and end each component of the pipeline with an assignment to the variable whose name is dot (`.`

) . We follow that with a semicolon.

```
1:8 ->.; sum(.) ->.; sqrt(.)
## [1] 6
```

**Update** Added info on expm package and simplified example at top. Added postlogic package.

**Update 2** The development version of R has defined a `|>`

pipe. Unlike magrittr’s `%>%`

it can only substitute into the first argument of the right hand side. Although limited, it works via syntax transformation so it has no performance impact.

Source: StackOverflow.com