Error: could not find function "%>%"
Asked Answered
D

4

151

I'm running an example in R, going through the steps and everything is working so far except for this code produces an error:

 words <- dtm %>%
 as.matrix %>%
 colnames %>%
 (function(x) x[nchar(x) < 20])

Error: could not find function "%>%"

I don't understand what the benefit of using this special operator %>% is, and any feedback would be great.

Digamma answered 14/5, 2015 at 22:33 Comment(2)
Related: #27126172Tamie
I am getting inconsistent results based on the responses. It is failing 8 out of 10 times but then running fine.Bunsen
O
214

You need to load a package (like magrittr or dplyr) that defines the function first, then it should work.

install.packages("magrittr") # package installations are only needed the first time you use it
install.packages("dplyr")    # alternative installation of the %>%
library(magrittr) # needs to be run every time you start R and want to use %>%
library(dplyr)    # alternatively, this also loads %>%

The pipe operator %>% was introduced to "decrease development time and to improve readability and maintainability of code."

But everybody has to decide for himself if it really fits his workflow and makes things easier. For more information on magrittr, click here.

Not using the pipe %>%, this code would return the same as your code:

words <- colnames(as.matrix(dtm))
words <- words[nchar(words) < 20]
words

EDIT: (I am extending my answer due to a very useful comment that was made by @Molx)

Despite being from magrittr, the pipe operator is more commonly used with the package dplyr (which requires and loads magrittr), so whenever you see someone using %>% make sure you shouldn't load dplyr instead.

Ogletree answered 14/5, 2015 at 22:38 Comment(4)
Also do you mean the code written above could be written in different way without the needs to use %>%Digamma
Despite being from magrittr, the pipe operator is more commonly used with the package dplyr (which requires and loads magrittr), so whenever you see someone using %>% make sure you shouldn't load dplyr instead.Donate
I would like to stress that only loading Tidyverse (library(tidyverse)) might not be enough. I have added (library(dplyr)) after library(tidyverse) and then %>% was found.Snowblink
So jelous that this got 171 upvotes. Crazy reputation pointsStratosphere
H
20

On Windows: if you use %>% inside a %dopar% loop, you have to add a reference to load package dplyr (or magrittr, which dplyr loads).

Example:

plots <- foreach(myInput=iterators::iter(plotCount), .packages=c("RODBC", "dplyr")) %dopar%
{
    return(getPlot(myInput))
}

If you omit the .packages command, and use %do% instead to make it all run in a single process, then works fine. The reason is that it all runs in one process, so it doesn't need to specifically load new packages.

Hellenistic answered 4/6, 2017 at 9:15 Comment(3)
This is a good point and worth highlighting specifically in relation to foreach. I've had issues in the past with this, and it's a different problem to the one solved by the accepted answer.Kristinakristine
I'm not sure if this is the right venue to highlight this also, but I've suggested and edit based on the iterators package which simplifies the foreach loop. Feel free to disregard if this is inappropriate.Kristinakristine
Thanks. Saved me. I had no idea I needed to declare the dependencies in the foreach.Potentate
I
7

The pipe operator is not available in base R. You need to load one of the following packages to use it: dplyr, tidyverse or magrittr

Imperceptive answered 16/3, 2022 at 1:40 Comment(0)
S
4

One needs to install magrittr as follows

install.packages("magrittr")

Then, in one's script, don't forget to add on top

library(magrittr)

For the meaning of the operator %>% you might want to consider this question: What does %>% function mean in R?

Note that the same operator would also work with the library dplyr, as it imports from magrittr.

dplyr used to have a similar operator (%.%), which is now deprecated. Here we can read about the differences between %.% (deprecated operator from the library dplyr) and %>% (operator from magrittr, that is also available in dplyr)

Sundries answered 1/2, 2021 at 20:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.