loading dplyr after plyr is causing issues
Asked Answered
S

2

5

Test Case:

library(dplyr)
library(plyr)
library(dplyr)
mtcars%>%rename(x=gear)

This gives error. Any help would be greatly appreciated.

Suffragist answered 27/7, 2015 at 3:55 Comment(5)
As above, or restart R session, then load dplyr first. Consider using ddplyer or other newer versions that play together betterAlkanet
You can also access a specific package by including the package name in the function call. For example, even if you load plyr after dplyr, you can do mtcars %>% dplyr::rename(x=gear) to use dplyr's rename function. However, it's generally better to load plyr before dplyr (assuming you want to use functions from both packages), since dplyr is faster. Or, you can just load dplyr and then use, for example, plyr::rename if you want a specific plyr function that's masked by dplyr.Subminiature
Thanks for the comments. The issue is that this is part of a large codebase with multiple developers. So detaching may not be practical.Suffragist
@nongkrong it's never a good idea to use detach()Chuch
@nongkrong I don't do that ;)Chuch
S
12

Based on @hadley's tweet. Best answer is to load plyr ALWAYS before dplyr, AND not load plyr again. Pasting his tweet for reference.


Hadley Wickham ‏@hadleywickham  Jul 27
@gunapemmaraju just load plyr before dplyr?
Suffragist answered 29/7, 2015 at 8:20 Comment(1)
I think you mean and not load dplyr againPotation
L
5

I have this problem when require plyr again sourcing files. You can do

if("dplyr" %in% (.packages())){
          detach("package:dplyr", unload=TRUE) 
          detach("package:plyr", unload=TRUE) 
} 
library(plyr)
library(dplyr)
Ladida answered 8/2, 2017 at 13:19 Comment(1)
Wow I tried unloading and loading again manually in every different order but it wasn't working, but this code fixed my problem! Thank you!Annalisaannalise

© 2022 - 2024 — McMap. All rights reserved.