Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: could not find function "%>%"

Tags:

r

dplyr

magrittr

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.

like image 592
Haidar Avatar asked May 14 '15 22:05

Haidar


People also ask

What does could not find function %>% in R mean?

July 15, 2021. Error in R – could not find function “%>%” – means that you don't have loaded or installed the R package that is using that. The same is with any other “could not find function” R error. One of the most popular R packages that are using %>% or pipe operator is dplyr.

What is the %>% function in R?

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

Why can't RStudio find my function?

This error usually occurs when a package has not been loaded into R via library . R does not know where to find the specified function. It's a good habit to use the library functions on all of the packages you will be using in the top R chunk in your R Markdown file, which is usually given the chunk name setup .

What does the pipe operator %>% do?

What does the pipe do? The pipe operator, written as %>% , has been a longstanding feature of the magrittr package for R. It takes the output of one function and passes it into another function as an argument. This allows us to link a sequence of analysis steps.


2 Answers

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.

like image 143
maRtin Avatar answered Oct 13 '22 14:10

maRtin


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.

like image 20
Contango Avatar answered Oct 13 '22 14:10

Contango