R/2_attach.R

Defines functions metid_package_version metid_attach same_library metid_core_unloaded

core <-
  c(
    "masstools", "massdataset", "ggplot2", "dplyr", "magrittr",
    "MSnbase"
  )

metid_core_unloaded <- function() {
  search <- paste0("package:", core)
  core[!search %in% search()]
}

# Attach the package from the same package library it was
same_library <- function(pkg) {
  loc <-
    if (pkg %in% loadedNamespaces())
      dirname(getNamespaceInfo(pkg, "path"))
  do.call("library",
          list(
            pkg,
            lib.loc = loc,
            character.only = TRUE,
            warn.conflicts = FALSE
          ))
}

metid_attach <- function() {
  to_load <- metid_core_unloaded()
  if (length(to_load) == 0)
    return(invisible())
  
  # msg(cli::rule(
  #   left = crayon::bold("Attaching packages"),
  #   right = paste0("metid ", metid_package_version("metid"))
  # ),
  # startup = TRUE)
  
  versions <-
    vapply(to_load, metid_package_version, character(1))
  packages <- paste0(
    crayon::green(cli::symbol$tick),
    " ",
    crayon::blue(format(to_load)),
    " ",
    crayon::col_align(versions, max(crayon::col_nchar(versions)))
  )
  
  if (length(packages) %% 2 == 1) {
    packages <- append(packages, "")
  }
  col1 <- seq_len(length(packages) / 2)
  info <- paste0(packages[col1], "     ", packages[-col1])
  
  # msg(paste(info, collapse = "\n"), startup = TRUE)
  
  suppressPackageStartupMessages(lapply(to_load, same_library))
  
  invisible()
}

metid_package_version <- function(x) {
  version <- as.character(unclass(utils::packageVersion(x))[[1]])
  
  if (length(version) > 3) {
    version[4:length(version)] <-
      crayon::red(as.character(version[4:length(version)]))
  }
  paste0(version, collapse = ".")
}
tidymass/metid documentation built on Oct. 8, 2024, 10:32 p.m.