- Notifications
You must be signed in to change notification settings - Fork15
An alternative conflict resolution strategy for R
License
Unknown, MIT licenses found
Licenses found
r-lib/conflicted
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
The goal of conflicted is to provide an alternative conflict resolutionstrategy. R’s default conflict resolution system gives precedence to themost 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 errorand forcing you to choose which function to use.
Thanks to@krlmlr for this neat idea! Thiscode was previously part of the experimentalstrict package, but I decidedimproved conflict resolution is useful by itself and worth its ownpackage.
# install.packages("pak")pak::pak("r-lib/conflicted")
To use conflicted, all you need to do is load it:
library(conflicted)library(dplyr)filter(mtcars,cyl==8)#> Error:#> ! [conflicted] filter found in 2 packages.#> Either pick the one you want with `::`:#> • dplyr::filter#> • stats::filter#> Or declare a preference with `conflicts_prefer()`:#> • `conflicts_prefer(dplyr::filter)`#> • `conflicts_prefer(stats::filter)`
As suggested, you can either namespace individual calls:
dplyr::filter(mtcars,am&cyl==8)#> mpg cyl disp hp drat wt qsec vs am gear carb#> Ford Pantera L 15.8 8 351 264 4.22 3.17 14.5 0 1 5 4#> Maserati Bora 15.0 8 301 335 3.54 3.57 14.6 0 1 5 8
Or declare a session-wide preference:
conflicts_prefer(dplyr::filter())#> [conflicted] Will prefer dplyr::filter over any other package.filter(mtcars,am&cyl==8)#> mpg cyl disp hp drat wt qsec vs am gear carb#> Ford Pantera L 15.8 8 351 264 4.22 3.17 14.5 0 1 5 4#> Maserati Bora 15.0 8 301 335 3.54 3.57 14.6 0 1 5 8
I recommend declaring preferences directly underneath the correspondinglibrary call:
library(dplyr)conflicts_prefer(dplyr::filter)
You can ask conflicted to report any conflicts in the current session:
conflict_scout()#> 1 conflict#> • `lag()`: dplyr and stats
Functions surrounded by[] have been chosen using one of the built-inrules. Herefilter() has been selected because of the preferencedeclared above; the set operations have been selected because theyfollow the superset principle and extend the API of the baseequivalents.
Loading conflicted creates a new “conflicted” environment that isattached just after the global environment. This environment contains anactive binding for any object that is exported by multiple packages; theactive binding will throw an error message describing how todisambiguate the name. The conflicted environment also contains bindingsforlibrary() andrequire() that suppress conflict reporting andupdate the conflicted environment with any new conflicts.
It is worth comparing conflicted toboxandimport. Both packages providestrict alternatives tolibrary(), giving much finer control over whatfunctions are added to the search path.
# box expects you to either namespace all package functions or to load them explicitlybox::use(dplyr)dplyr$filter(mtcars,cyl==8)# or:box::use(dplyr[select,arrange,dplyr_filter=filter])dplyr_filter(mtcars,cyl==8)# import expects you to explicitly load functionsimport::from(dplyr,select,arrange,dplyr_filter=filter)dplyr_filter(mtcars,cyl==8)
These require more upfront work than conflicted, in return for greaterprecision and control.
Since conflicted was created base R also improved its tools for managingsearch path conflicts. Seethe blogpostby Luke Tierney for details. The main difference is that base R requiresup front conflict resolution of all functions when loading a package;conflicted only reports problems as you use conflicted functions.
Please note that the conflicted project is released with aContributorCode of Conduct. Bycontributing to this project, you agree to abide by its terms.
About
An alternative conflict resolution strategy for R
Topics
Resources
License
Unknown, MIT licenses found
Licenses found
Code of conduct
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Contributors8
Uh oh!
There was an error while loading.Please reload this page.