dplyr::select function clashes with MASS::select

As Pascal said, the following works

require(MASS)
require(dplyr)
mtcars %>%
   dplyr::select(mpg)

This happens to me more frequently than I should admit. dplyr clashes with MASS::select, plyr::summarise and stats::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 with plyr.

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 by recode and alpha, 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:

detach("package:dplyr", character.only = TRUE)
library("dplyr", character.only = TRUE)

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:

devtools::install_github("rubenarslan/formr")

If you load first the MASS library and second the dplyr one

library (MASS)
library (dplyr)

then the first version of the select function in your session searchpaths () will be the one in the dplyr library.

Hence

select(mtcars, mpg)

will work as

dplyr::select(mtcars, mpg)

Tags:

R

Dplyr