Alec Y.
Alec Y.

Reputation: 383

Using %>% operator from dplyr without loading dplyr in R

I'm currently building a package and I was wondering if there was a way to call the %>% operator from dplyr without actually attaching the dplyr package. For example, with any function that is being exported from a package, you can call it with the double colon (::). So if I wanted to use the group_by function without attaching dplyr, I would enter dplyr::group_by. Is there something similar for operators?

Upvotes: 28

Views: 5226

Answers (4)

There seems to be a clash between the pipe notation as implemented in dplyr and the double colon notation. I didn't find anything about it in the documentation.

## This works:
base::mean(1:10)

## This doesn't:
1:10 %>% base::mean

## It works after parenthesizing though:
1:10 %>% (base::mean)

It could be made a bit more prominent in the docs. This issue was moved by romainfrancois from tidyverse/dplyr#3958.

source : https://github.com/tidyverse/magrittr/issues/183

Upvotes: 0

Gregor Thomas
Gregor Thomas

Reputation: 145765

The easiest way would be to load the magrittr package, which only does piping, and is the original source of %>%. If you don't want to load any packages, you can still use %>%, but not in any really useful way (unless you define it in your environment as Andrie suggests). Using it with :: would go like this:

# standard use
mtcars %>% summary()
# :: use
magrittr::"%>%"(mtcars, summary())

You really lose any advantage of readability/not nesting with this method.

Since you say you're building a package, you should put magrittr in Imports, or even just us importsFrom and grab the "%>%" function. See here for more info.

Upvotes: 10

IRTFM
IRTFM

Reputation: 263331

If you have 'dplyr' installed but not loaded you can get a result with:

 dplyr::`%>%`   # Note the backticks, although quotes work as well.

That displays the code but at the bottom you will see that its environment is actually the NAMESPACE of 'magritter' which 'dplyr' imports. As the two other knowledgeable respondents point out there are a couple of ways to use it as a function although it cannot just be interposed between a lhs and a rhs argument unless you make a local copy that has the flanking "%"'s or call it with the functional parentheses. The R parser won't allow:

>  mtcars dplyr::"%>%" summary()
Error: unexpected symbol in " mtcars dplyr"

Upvotes: 3

Andrie
Andrie

Reputation: 179408

You can refer to any object with non-standard name by enclosing in backticks. This means you can do this:

`%>%` <- magrittr::`%>%`

This will define the %>% operator in your current environment. For example:

iris %>% head

  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa

Upvotes: 50

Related Questions