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?
%>% 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).
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 .
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With