-
Notifications
You must be signed in to change notification settings - Fork 14
/
README.Rmd
100 lines (69 loc) · 4.1 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
set.seed(1014)
```
# conflicted
<!-- badges: start -->
[![R-CMD-check](https://github.com/r-lib/conflicted/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/r-lib/conflicted/actions/workflows/R-CMD-check.yaml)
[![Codecov test coverage](https://codecov.io/gh/r-lib/conflicted/branch/main/graph/badge.svg)](https://app.codecov.io/gh/r-lib/conflicted?branch=main)
<!-- badges: end -->
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](https://github.com/krlmlr) for this neat idea! This code was previously part of the experimental [strict](https://github.com/hadley/strict) package, but I decided improved conflict resolution is useful by itself and worth its own package.
## Installation
```{r, eval = FALSE}
# install.packages("pak")
pak::pak("r-lib/conflicted")
```
## Usage
To use conflicted, all you need to do is load it:
```{r, error = TRUE}
library(conflicted)
library(dplyr)
filter(mtcars, cyl == 8)
```
As suggested, you can either namespace individual calls:
```{r}
dplyr::filter(mtcars, am & cyl == 8)
```
Or declare a session-wide preference:
```{r}
conflicts_prefer(dplyr::filter())
filter(mtcars, am & cyl == 8)
```
I recommend declaring preferences directly underneath the corresponding library call:
```{r, eval = FALSE}
library(dplyr)
conflicts_prefer(dplyr::filter)
```
You can ask conflicted to report any conflicts in the current session:
```{r}
conflict_scout()
```
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](https://github.com/klmr/box) and [import](https://github.com/rticulate/import). Both packages provide strict alternatives to `library()`, giving much finer control over what functions are added to the search path.
```{r, eval = FALSE}
# box expects you to either namespace all package functions or to load them explicitly
box::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 functions
import::from(dplyr, select, arrange, dplyr_filter = filter)
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](https://developer.r-project.org/Blog/public/2019/03/19/managing-search-path-conflicts/) 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](https://conflicted.r-lib.org/CODE_OF_CONDUCT.html). By contributing to this project, you agree to abide by its terms.