#' Conflicts between finRes and other packages
#'
#' This function lists all the conflicts between packages in finRes
#' and other packages that you have loaded.
#'
#' @export
#' @examples
#' finRes_conflicts()
finRes_conflicts <- function() {
envs <- purrr::set_names(search())
objs <- invert(lapply(envs, ls_env))
conflicts <- purrr::keep(objs, ~ length(.x) > 1L)
tidy_names <- paste0("package:", finRes_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 = "finRes_conflicts")
}
finRes_conflict_message <- function(x) {
if (length(x) == 0L) return("")
header <- cli::rule(
left = crayon::bold("Conflicts"),
right = "finRes_conflicts()"
)
pkgs <- x %>% purrr::map(~ gsub("^package:", "", .))
others <- pkgs %>% purrr::map(`[`, -1L)
other_calls <- purrr::map2_chr(
others, names(others),
~ paste0(crayon::blue(.x), "::", .y, "()", collapse = ", ")
)
winner <- pkgs %>% purrr::map_chr(1L)
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.finRes_conflicts <- function(x, ..., startup = FALSE) {
cli::cat_line(finRes_conflict_message(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) <= 1L)
return()
# Remove identical functions
objs <- objs[!duplicated(objs)]
packages <- packages[!duplicated(packages)]
if (length(objs) == 1L)
return()
packages
}
ls_env <- function(env) {
x <- ls(pos = env)
if (identical(env, "package:dplyr")) {
x <- setdiff(x, c("intersect", "setdiff", "setequal", "union"))
}
x
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.