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.
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.
%>% 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.
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 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.
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 packagedplyr
(which requires and loadsmagrittr
), so whenever you see someone using%>%
make sure you shouldn't loaddplyr
instead.
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.
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