Search code examples
rmagrittr

Should I avoid programming packages with pipe operators?


Are there any objective reasons for why pipe operators from the R package magrittr, such as %>%, should be avoided when I program packages in R?

More specifically, I want to know if using pipe operators might cause coding conflicts or (positively or negatively) affect performance. I am looking for specific, concrete examples of such cases.


Solution

  • Like all advanced functions written in R, %>% carries a lot of overhead, so don't use it in loops (this includes implicit loops, such as the *apply family, or the per group loops in packages like dplyr or data.table). Here's an example:

    library(magrittr)
    x = 1:10
    
    system.time({for(i in 1:1e5) identity(x)})
    #   user  system elapsed 
    #   0.07    0.00    0.08 
    system.time({for(i in 1:1e5) x %>% identity})
    #   user  system elapsed 
    #  15.39    0.00   16.68