-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prefer multiple functions with convenient syntax (#82)
Fixes #77
- Loading branch information
Showing
12 changed files
with
154 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#' Declare many preferences at once | ||
#' | ||
#' @description | ||
#' `conflicts_prefer()` allows you to declare "winners" of conflicts, | ||
#' declaring one or many winners at once. | ||
#' | ||
#' See [conflict_prefer()] for more more precise control. | ||
#' | ||
#' @section Best practices: | ||
#' I recommend placing a single call to `conflicts_prefer()` at the top of | ||
#' your script, immediately after loading all needed packages with calls to | ||
#' `library()`. | ||
#' | ||
#' @export | ||
#' @param ... Functions to prefer in form `pkg::fun` or `pkg::fun()`. | ||
#' @inheritParams conflict_prefer | ||
#' @examples | ||
#' conflicts_prefer( | ||
#' dplyr::filter(), | ||
#' dplyr::lag(), | ||
#' ) | ||
conflicts_prefer <- function(..., .quiet = FALSE) { | ||
calls <- exprs(...) | ||
# accept pkg::fun() or pkg::fun | ||
calls <- lapply(calls, standardise_call) | ||
|
||
is_ok <- vapply(calls, is_ns_call, logical(1)) | ||
if (any(!is_ok)) { | ||
cli::cli_abort( | ||
"All arguments must be in form {.code pkg::fun} or {.fn pkg::fun}" | ||
) | ||
} | ||
|
||
pkgs <- vapply(calls, function(x) as.character(x[[2]]), character(1)) | ||
funs <- vapply(calls, function(x) as.character(x[[3]]), character(1)) | ||
|
||
for (i in seq_along(calls)) { | ||
conflict_preference_register(funs[[i]], pkgs[[i]], quiet = .quiet) | ||
} | ||
|
||
conflicts_register() | ||
|
||
invisible() | ||
} | ||
|
||
standardise_call <- function(x) { | ||
if (is_call(x, n = 0)) { | ||
x[[1]] | ||
} else { | ||
x | ||
} | ||
} | ||
|
||
is_ns_call <- function(x) { | ||
is_call(x, "::", n = 2) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# trailing () is optional | ||
|
||
Code | ||
conflicts_prefer(dplyr::lag, dplyr::filter()) | ||
Message | ||
[conflicted] Will prefer dplyr::lag over any other package. | ||
[conflicted] Will prefer dplyr::filter over any other package. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
test_that("trailing () is optional", { | ||
withr::defer(prefs_reset()) | ||
|
||
expect_snapshot({ | ||
conflicts_prefer( | ||
dplyr::lag, | ||
dplyr::filter() | ||
) | ||
}) | ||
}) |