R/install.R

Defines functions install_package_plan read_install_plan

Documented in install_package_plan

#' Install packages from a package installation plan
#'
#' Download and install packages from a package installation plan.
#'
#' It uses [download_package_plan()] to download the packages, if the plan
#' does not have a `"file"` column.
#'
#' It downloads and installs all packages in parallel.
#'
#' @param plan Package installation plan object, as created by the
#'   pkgdepends package. It can also be a character scalar referring to
#'   to an exported installation plan JSON file, in which case the plan
#'   is loaded from that file.
#' @param lib Package library to install the packages to. If it does not
#'   exist, it will be created.
#' @param num_workers The number of processes to use to install the
#'   packages. If it is the string scalar `"auto"`, then it is determined
#'   automatically, from the `"Ncpus"` option, if set, or the number
#'   of cores the machine has.
#' @param quiet If `TRUE`, suppress status messages.
#' @return The installation plan.
#'
#' @export
#' @seealso [download_package_plan()] if you only want to download the
#'   files.

install_package_plan <- function(plan = "resolution.json",
                                 lib = .libPaths()[[1]],
                                 num_workers = "auto",
                                 quiet = FALSE) {
  start <- Sys.time()

  if (is.character(plan)) plan <- read_install_plan(plan)

  if (identical(num_workers, "auto")) num_workers <- get_num_cores()

  required_columns <- c(
    "ref", "package", "type", "direct", "binary", "dependencies",
    "vignettes", "needscompilation", "metadata", "sources", "target")
  stopifnot(
    inherits(plan, "data.frame"),
    all(required_columns %in% colnames(plan)),
    is_string(lib)
  )

  mkdirp(lib)

  if (is.null(plan[["file"]])) plan <- download_package_plan(plan, quiet)

  repo_dir <- unique(plan$download_dir)
  if (length(repo_dir) == 0) stop("Cannot find `download_dir` in plan")
  if (length(repo_dir) > 1) {
    stop("Packages need to be downloaded into the same `download_dir`")
  }

  result <- install_package_plan_parallel(
    plan, lib, repo_dir, num_workers, quiet )

  took <- Sys.time() - start

  if (!quiet) message("\n---> Done in ", pretty_dt(took), "\n")

  invisible(result)
}

read_install_plan <- function(file) {
  obj <- json$parse_file(file)
  mat <- do.call(rbind, obj)
  df <- as.data.frame(mat, stringsAsFactors = FALSE)
  keep_list_cols <- c("dependencies", "metadata", "sources")
  for (col in setdiff(colnames(df), keep_list_cols)) {
    df[[col]] <- unlist(df[[col]])
  }
  df$dependencies <- lapply(df$dependencies, unlist_chr)
  df$sources <- lapply(df$sources, unlist_chr)
  df$metadata <- lapply(df$metadata, unlist_chr)
  df
}
gaborcsardi/installlite documentation built on May 22, 2019, 5:33 p.m.