If I load the MASS
package:
library(MASS)
then load try to run dplyr::select
, I get a error:
library(dplyr)
mtcars %.%
select(mpg)
# Error in select(`__prev`, mpg) : unused argument (mpg)
How can I use dplyr::select
with the MASS
package loaded?
This happens to me more frequently than I should admit. dplyr clashes with
MASS::select
,plyr::summarise
andstats::filter
among other things, especially when loading packages which load one of those libraries via library (they shouldn't, but some still do) or when you load dplyr in your.Rprofile
(don't!). And it can lead to pretty obscure problems, not always an error message, especially conflicts withplyr
.I only recently learned about the
conflicts()
function. It's useful, but "over-reports" conflicts when two packages have identical functions, e.g. tidyr::%>%
and dplyr::%>%
.So I wrote a function to tell me if I'm going mad or whether there's actually a conflict causing the current bug. It not only checks for conflicts, it checks whether a certain desired package is the one "on top" and whether the function's bodies actually differ.
It does this for dplyr by default, but you can specify another package using the
want_package
parameter. For example, I often get tripped up byrecode
andalpha
, which are reused in many packages.Usage is simply:
amigoingmad()
.By default, it will also automatically "fix" things if dplyr is not "on top", using the following commands:
Note that the function will report if a user-specified function is blocking dplyr, but does not fix this automatically for safety's sake (just remove the function in that case).
As of yet, this solution hasn't caused any problems to me. Of course I wouldn't advocate using this in production code, but when you're debugging an
.Rmd
-file and may have messed up the load order by accident it's a quick way to find out.If you want this in a package:
As Pascal said, the following works
If you load first the
MASS
library and second thedplyr
onethen the first version of the
select
function in your sessionsearchpaths ()
will be the one in thedplyr
library.Hence
will work as