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.

2

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

4

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.

3

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)

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

Anyone else stumbling upon this for calculating powers of matrices please install this library (dplyr alone not correct)

library(expm)