Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

operators

r

dplyr

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?

like image 780
Alec Y. Avatar asked Dec 09 '14 18:12

Alec Y.


People also ask

What does %>% mean in dplyr?

%>% is called the forward pipe operator in R. It provides a mechanism for chaining commands with a new forward-pipe operator, %>%. This operator will forward a value, or the result of an expression, into the next function call/expression. It is defined by the package magrittr (CRAN) and is heavily used by dplyr (CRAN).

What is pipe in dplyr?

Pipes let you take the output of one function and send it directly to the next, which is useful when you need to many things to the same data set. Pipes in R look like %>% and are made available via the magrittr package installed as part of dplyr .

What does %>% mean in Tidyverse?

Use %>% 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.


1 Answers

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 
like image 85
Andrie Avatar answered Oct 05 '22 04:10

Andrie