Detect conflicts between packages in R [duplicate]

只谈情不闲聊 提交于 2019-11-30 23:59:42

问题


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?


回答1:


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




回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/15949298/detect-conflicts-between-packages-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!