R/reinstall.R

#' Reinstall and load package from Github repo
#'
#' Unloads, uninstalls, reinstalls, and loads package from Github repo for use during package development.
#' @param pkg Development package
#' @param repo Github repo to reinstall from.
#' @param pkg.load Should package be attached after installation? Defaults to \code{TRUE}.
#' @param anc.pkgs Character vector of other packages to load.  Defaults to 'saxy'.
#' @param verbose Show function progress? Logical. Defaults to \code{TRUE}.
#' @param inst.verbose Show progress from \code{devtools::install_github}? Defaults to \code{TRUE}.
#' @export
#' @return NULL
#' @examples
#' reinstall(repo = 'SPractice')


reinstall <- function(pkg,
                      repo     = 'ssaxe-usgs',
                      pkg.load = T,
                      anc.pkgs = c('saxy'),
                      verbose  = T,
                      inst.verbose = T){
  # Detach package if attached
  if (pkg %in% (.packages())){
    if (verbose) cat(paste0("Detaching '", pkg, "'\n"))
    detach(name = paste0("package:", pkg),
           unload=TRUE,
           character.only = T)
  }
  # Remove package
  if (is.element(pkg, rownames(installed.packages()))){
    if (verbose) cat(paste0("Removing '", pkg, "'\n"))
    suppressMessages(remove.packages(pkgs = pkg))
  }else{
    if (verbose) cat("Skipping 'remove.package', no package not installed\n")
  }

  # Attach devtools library
  suppressMessages(require(devtools))
  # Install from repo
  if (verbose) cat(paste0("Installing from '", paste0(repo, '/', pkg), "'\n"))
  devtools::install_github(repo = paste0(repo, '/', pkg),
                           quiet = inst.verbose)
  # Attach library again
  if (pkg.load){
    if (verbose) cat(paste0("Reattaching '", pkg, "'\n"))
    suppressMessages(require(package        = pkg,
                             character.only = T))
  }
  # Attach other packages
  if (!is.null(anc.pkgs)){
    if (verbose) cat(paste0("Attaching anc.pkgs: ", saxy::makeSentence(anc.pkgs), '.\n'))
    lapply(X              = anc.pkgs,
           FUN            = library,
           character.only = T)
  }
  # Done
  if (verbose) cat("Done!\n")
}
ssaxe-usgs/saxey documentation built on May 25, 2019, 5:02 a.m.