11

I've recently found out that errors can be caused due to conflicts between packages, that is, two (or more) packages might have functions named similarly. I know that the code search () produces the list of packages ordered in the way R reads them. There is also the args code which gives the function read by R.
What I would like to know firstly is how to detect if an error is being produced because of conflicts between packages and secondly how to find out which packages are conflicting? Finally, after the conflicts have been detected, how can we force R to use specifically the function from one of the packages?

Vara
  • 145
  • 1
  • 8

3 Answers3

9

If R loads a new package, it will produce a warning if that new package contains any functions that are already present in the working environment. So if you pay attention during package loading, you can see if there are any conflicts. When there are conflicts, you can force R to use the function from a particular package like this:

package_name::function_name
Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
  • I have just learned that dplyr won't work properly when plyr is loaded also, not even in the correct order as is suggested by the following: "You have loaded plyr after dplyr - this is likely to cause problems. If you need functions from both plyr and dplyr, please load plyr first, then `dplyr:library(plyr); library(dplyr)`". I tried reloading dplyr again after plyr as well as clearing the environment, but the only solution was to restart R Studio. We use plyr from legacy code. #Michele, since I don't understand your code, I'm hoping to just eliminate the use of plyr. – W Barker Mar 01 '22 at 00:53
9

As @Paul says, when attaching (e.g. via library function) a package you may get:

> library("gdata", lib.loc="C:/Program Files/R/R-2.15.3/library")
gdata: read.xls support for 'XLS' (Excel 97-2004) files ENABLED.

gdata: read.xls support for 'XLSX' (Excel 2007+) files ENABLED.

Attaching package: ‘gdata’

The following object(s) are masked from ‘package:stats’:

    nobs

The following object(s) are masked from ‘package:utils’:

    object.size

When you get "The following object(s) are masked" mean that calls to those function will be thought by R as calls to the functions in the new package, in my example gdata.

You can avoid this via:

> nobs
function (object, ...) 
UseMethod("nobs")
<environment: namespace:gdata>
> stats::nobs
function (object, ...) 
UseMethod("nobs")
<bytecode: 0x0000000008a92790>
<environment: namespace:stats

hope that helps

Michele
  • 8,563
  • 6
  • 45
  • 72
9

I think you're looking for getAnywhere which will tell you all the places its argument exists. E.g. (in my current load set):

Rgames> getAnywhere(logit)
2 differing objects matching ‘logit’ were found
in the following places
  package:boot
  package:pracma
  namespace:boot
  namespace:pracma
Use [] to view one of them
Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73
  • Thanks for the answer Carl, I didn't know about this code. However, I think it's helpful when you know beforehand exactly which argument conflicts. – Vara Apr 11 '13 at 17:46