R/utils.R

Defines functions style_grey invert fpp2_packages text_col msg

Documented in fpp2_packages

# Based on utils.R from the tidyverse package

msg <- function(..., startup = FALSE) {
  if (startup) {
    if (!isTRUE(getOption("fpp2.quiet"))) {
      packageStartupMessage(text_col(...))
    }
  } else {
    message(text_col(...))
  }
}

text_col <- function(x) {
  # If RStudio not available, messages already printed in black
  if (!rstudioapi::isAvailable()) {
    return(x)
  }

  if (!rstudioapi::hasFun("getThemeInfo")) {
    return(x)
  }

  theme <- rstudioapi::getThemeInfo()

  if (isTRUE(theme$dark)) crayon::white(x) else crayon::black(x)
}

#' List all packages loaded by fpp2
#'
#' @param include_self Include fpp2 in the list?
#' @return A character vector of package names.
#' @export
#' @examples
#' fpp2_packages()
fpp2_packages <- function(include_self = FALSE) {
  raw <- utils::packageDescription("fpp2")$Imports
  imports <- strsplit(raw, ",")[[1]]
  parsed <- gsub("^\\s+|\\s+$", "", imports)
  names <- vapply(strsplit(parsed, "\\s+"), "[[", 1, FUN.VALUE = character(1))

  if (include_self) {
    names <- c(names, "fpp2")
  }

  names
}

invert <- function(x) {
  if (length(x) == 0) {
    return()
  }
  stacked <- utils::stack(x)
  tapply(as.character(stacked$ind), stacked$values, list)
}


style_grey <- function(level, ...) {
  crayon::style(
    paste0(...),
    crayon::make_style(grDevices::grey(level), grey = TRUE)
  )
}
robjhyndman/fpp documentation built on July 31, 2023, 2:14 a.m.