R/packages.R

Defines functions require_quiet save_my_packages load_my_packages

Documented in load_my_packages save_my_packages

#' Save and load packages from file
#'
#' @param path The path to a text file containing one package per line. If
#'   `NULL` (default), then the default list is read from `k5/inst/PACKAGES`.
#' @param x A character vector of package names to save. If `NULL` (default),
#'   use all currently attached packages.
#' @return The list of packages, invisibly.
#' @export
load_my_packages <- function(path = NULL) {
  if (is.null(path)) {
    path <- system.file("PACKAGES", package = "k5", mustWork = TRUE)
  }
  x <- readLines(path)
  is_loaded <- vapply(x, require_quiet, logical(1))
  x <- x[is_loaded]
  usethis::ui_done("load {length(x)} packages from {usethis::ui_path(path)}")
  invisible(x)
}

#' @rdname load_my_packages
#' @export
save_my_packages <- function(x = NULL, path = tempfile()) {
  if (is.null(path)) {
    path <- system.file("PACKAGES", package = "k5", mustWork = TRUE)
  }
  if (is.null(x)) {
    x <- search()
    x <- sub("^package:", "", x[grepl("^package:", x), drop = FALSE])
  }
  writeLines(x, path)
  usethis::ui_done("saved {length(x)} packages to {usethis::ui_path(path)}")
  invisible(x)
}

require_quiet <- function(...) {
  suppressPackageStartupMessages(require(..., character.only = TRUE))
}

Try the k5 package in your browser

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

k5 documentation built on May 29, 2024, 7:48 a.m.