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
|
#' Conflicts between the tidyverse and other packages
#'
#' This function lists all the conflicts between packages in the tidyverse
#' and other packages that you have loaded.
#'
#' There are four conflicts that are deliberately ignored: \code{intersect},
#' \code{union}, \code{setequal}, and \code{setdiff} from dplyr. These functions
#' make the base equivalents generic, so shouldn't negatively affect any
#' existing code.
#'
#' @export
#' @examples
#' tidyverse_conflicts()
tidyverse_conflicts <- function() {
envs <- grep("^package:", search(), value = TRUE)
envs <- purrr::set_names(envs)
objs <- invert(lapply(envs, ls_env))
conflicts <- purrr::keep(objs, ~ length(.x) > 1)
tidy_names <- paste0("package:", tidyverse_packages())
conflicts <- purrr::keep(conflicts, ~ any(.x %in% tidy_names))
conflict_funs <- purrr::imap(conflicts, confirm_conflict)
conflict_funs <- purrr::compact(conflict_funs)
structure(conflict_funs, class = "tidyverse_conflicts")
}
tidyverse_conflict_message <- function(x) {
if (length(x) == 0) return("")
header <- cli::rule(
left = crayon::bold("Conflicts"),
right = "tidyverse_conflicts()"
)
pkgs <- x %>% purrr::map(~ gsub("^package:", "", .))
others <- pkgs %>% purrr::map(`[`, -1)
other_calls <- purrr::map2_chr(
others, names(others),
~ paste0(crayon::blue(.x), "::", .y, "()", collapse = ", ")
)
winner <- pkgs %>% purrr::map_chr(1)
funs <- format(paste0(crayon::blue(winner), "::", crayon::green(paste0(names(x), "()"))))
bullets <- paste0(
crayon::red(cli::symbol$cross), " ", funs,
" masks ", other_calls,
collapse = "\n"
)
paste0(header, "\n", bullets)
}
#' @export
print.tidyverse_conflicts <- function(x, ..., startup = FALSE) {
cli::cat_line(tidyverse_conflict_message(x))
invisible(x)
}
#' @importFrom magrittr %>%
confirm_conflict <- function(packages, name) {
# Only look at functions
objs <- packages %>%
purrr::map(~ get(name, pos = .)) %>%
purrr::keep(is.function)
if (length(objs) <= 1)
return()
# Remove identical functions
objs <- objs[!duplicated(objs)]
packages <- packages[!duplicated(packages)]
if (length(objs) == 1)
return()
packages
}
ls_env <- function(env) {
x <- ls(pos = env)
if (identical(env, "package:dplyr")) {
x <- setdiff(x, c("intersect", "setdiff", "setequal", "union"))
}
x
}
|