0

I am working on a large project with many libraries. I am running into a function conflict with dplyr::select(). Clearly there is another library loaded somewhere that also has a select() function. How can I figure out which one?

> iris %>% select("Species")
Error in select(., "Species") : unused argument ("Species")

(I know in this particular case the conflict is caused by MASS::select() and I can avoid it with dplyr::select("Species") but I want to know how to tell which package R is going to in general when there is a conflict.)

This question is different from what function/package conflicts with dplyr in R? because I am asking more generally how to find the cause of a function conflict, not the specific cause of the select() conflict. Some answers may be the same but readers will not know that from the question title.

jtr13
  • 1,225
  • 11
  • 25
  • 1
    Try `getAnywhere(x = "select")` – Matt Nov 02 '19 at 14:11
  • 2
    See [list of masked functions in R](https://stackoverflow.com/questions/6354697/list-of-masked-functions-in-r) – deepseefan Nov 02 '19 at 14:16
  • I get this particular conflict with `MASS`, which could be loaded by some other package. – bzki Nov 02 '19 at 15:18
  • 1
    If you type in `select`, after dumping the function it ends with something like ``, where `...` indicates the package in which the function was found (per the search path). Alternatively, you can bypass seeing the function body with `environment(select)`, which should just tell you the environment/namespace. – r2evans Nov 02 '19 at 15:25
  • Wow, these functions are game changers... wish I knew them earlier! – jtr13 Nov 02 '19 at 15:32
  • 1
    @r2evans Perfect, that is the simplest, most direct approach. – jtr13 Nov 02 '19 at 15:33
  • 1
    @NelsonGon The dupe doesn't answer the question, like the OP mentions in his last paragraph. – Rui Barradas Nov 02 '19 at 16:13
  • @Rui Barradas Retracted! Original post was edited I guess. In any case, the comments on that post are quite useful. Would be great if they made a community answer out of them. – NelsonGon Nov 02 '19 at 17:05
  • Yes, thanks, that was an edit in response to the dupe suggestion. – jtr13 Nov 02 '19 at 18:49

1 Answers1

5

The conflicted package is helpful, loaded either before or after the conflict occurs (or you know about it!)

> iris %>% select("Species")
Error in select(., "Species") : unused argument ("Species")
> library(conflicted)
> iris %>% select("Species")
Error: [conflicted] `select` found in 2 packages.
Either pick the one you want with `::`
* MASS::select
* dplyr::select
Or declare a preference with `conflict_prefer()`
* conflict_prefer("select", "MASS")
* conflict_prefer("select", "dplyr")

Base R also offers conflict awareness when libraries are loaded, described in the "Conflicts" section of the help page ?library. This can be a useful solution to resolving conflicts once they've been discovered.

Martin Morgan
  • 45,935
  • 7
  • 84
  • 112