R/conflicts.R

Defines functions fpp2_conflict_message fpp2_conflicts

Documented in fpp2_conflicts

# Based on conflicts.R from the tidyverse package

#' Conflicts between fpp2 packages and other packages
#'
#' This function lists all the conflicts between packages in the fpp2 collection
#' and other packages that you have loaded.
#'
#' Some conflicts are deliberately ignored: \code{intersect}, \code{union},
#' \code{setequal}, and \code{setdiff} from dplyr; and \code{intersect},
#' \code{union}, \code{setdiff}, and \code{as.difftime} from lubridate.
#' These functions make the base equivalents generic, so shouldn't negatively affect any
#' existing code.
#'
#' @return A list object of class \code{fpp2_conflicts}.
#' @export
#' @examples
#' fpp2_conflicts()
fpp2_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:", fpp2_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 = "fpp2_conflicts")
}

fpp2_conflict_message <- function(x) {
  if (length(x) == 0) {
    return("")
  }

  header <- cli::rule(
    left = crayon::bold("Conflicts"),
    right = "fpp2_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.fpp2_conflicts <- function(x, ..., startup = FALSE) {
  cli::cat_line(fpp2_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) <= 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"))
  }
  if (identical(env, "package:lubridate")) {
    x <- setdiff(x, c("intersect", "setdiff", "union", "as.difftime"))
  }
  x
}

Try the fpp2 package in your browser

Any scripts or data that you put into this service are public.

fpp2 documentation built on Sept. 13, 2020, 5:17 p.m.