项目作者: r-lib

项目描述 :
An alternative conflict resolution strategy for R
高级语言: R
项目地址: git://github.com/r-lib/conflicted.git
创建时间: 2018-05-20T23:37:42Z
项目社区:https://github.com/r-lib/conflicted

开源协议:Other

下载


conflicted

R-CMD-check
Codecov test
coverage

The goal of conflicted is to provide an alternative conflict resolution
strategy. R’s default conflict resolution system gives precedence to the
most recently loaded package. This can make it hard to detect conflicts,
particularly when introduced by an update to an existing package.
conflicted takes a different approach, making every conflict an error
and forcing you to choose which function to use.

Thanks to @krlmlr for this neat idea! This
code was previously part of the experimental
strict package, but I decided
improved conflict resolution is useful by itself and worth its own
package.

Installation

  1. # install.packages("pak")
  2. pak::pak("r-lib/conflicted")

Usage

To use conflicted, all you need to do is load it:

  1. library(conflicted)
  2. library(dplyr)
  3. filter(mtcars, cyl == 8)
  4. #> Error:
  5. #> ! [conflicted] filter found in 2 packages.
  6. #> Either pick the one you want with `::`:
  7. #> • dplyr::filter
  8. #> • stats::filter
  9. #> Or declare a preference with `conflicts_prefer()`:
  10. #> • `conflicts_prefer(dplyr::filter)`
  11. #> • `conflicts_prefer(stats::filter)`

As suggested, you can either namespace individual calls:

  1. dplyr::filter(mtcars, am & cyl == 8)
  2. #> mpg cyl disp hp drat wt qsec vs am gear carb
  3. #> Ford Pantera L 15.8 8 351 264 4.22 3.17 14.5 0 1 5 4
  4. #> Maserati Bora 15.0 8 301 335 3.54 3.57 14.6 0 1 5 8

Or declare a session-wide preference:

  1. conflicts_prefer(dplyr::filter())
  2. #> [conflicted] Will prefer dplyr::filter over any other package.
  3. filter(mtcars, am & cyl == 8)
  4. #> mpg cyl disp hp drat wt qsec vs am gear carb
  5. #> Ford Pantera L 15.8 8 351 264 4.22 3.17 14.5 0 1 5 4
  6. #> Maserati Bora 15.0 8 301 335 3.54 3.57 14.6 0 1 5 8

I recommend declaring preferences directly underneath the corresponding
library call:

  1. library(dplyr)
  2. conflicts_prefer(dplyr::filter)

You can ask conflicted to report any conflicts in the current session:

  1. conflict_scout()
  2. #> 1 conflict
  3. #> • `lag()`: dplyr and stats

Functions surrounded by [] have been chosen using one of the built-in
rules. Here filter() has been selected because of the preference
declared above; the set operations have been selected because they
follow the superset principle and extend the API of the base
equivalents.

How it works

Loading conflicted creates a new “conflicted” environment that is
attached just after the global environment. This environment contains an
active binding for any object that is exported by multiple packages; the
active binding will throw an error message describing how to
disambiguate the name. The conflicted environment also contains bindings
for library() and require() that suppress conflict reporting and
update the conflicted environment with any new conflicts.

Alternative approaches

It is worth comparing conflicted to box
and import. Both packages provide
strict alternatives to library(), giving much finer control over what
functions are added to the search path.

  1. # box expects you to either namespace all package functions or to load them explicitly
  2. box::use(dplyr)
  3. dplyr$filter(mtcars, cyl == 8)
  4. # or:
  5. box::use(dplyr[select, arrange, dplyr_filter = filter])
  6. dplyr_filter(mtcars, cyl == 8)
  7. # import expects you to explicitly load functions
  8. import::from(dplyr, select, arrange, dplyr_filter = filter)
  9. dplyr_filter(mtcars, cyl == 8)

These require more upfront work than conflicted, in return for greater
precision and control.

Since conflicted was created base R also improved its tools for managing
search path conflicts. See the blog
post

by Luke Tierney for details. The main difference is that base R requires
up front conflict resolution of all functions when loading a package;
conflicted only reports problems as you use conflicted functions.

Code of Conduct

Please note that the conflicted project is released with a Contributor
Code of Conduct
. By
contributing to this project, you agree to abide by its terms.