R/use.R

Defines functions use

Documented in use

#' @title Install new packages
#'
#' @description Installs new packages to be used into the project.
#' @details Similar to library but if the package is not yet loaded will ask user whether or not they want to load the package.
#'
#' @param pkg package name
#' @export
#'
#' @examples \dontrun{use(ggplot2)}
use <- function(pkg) {
  pkg <- as.character(substitute(pkg))
  if (!suppressWarnings(require(pkg, character.only = TRUE))) {
    msg <- paste("Install package", pkg, "(y/n)? ")
    answer <- readline(msg)
    answer <- substr(tolower(trimws(answer)), 1, 1)
    if (answer == "y") {
      install.packages(pkg)
      require(pkg, character.only = TRUE)
    } else {
      msg <- paste("Package", pkg, "not loaded.")
      message(msg)
    }
  }
}
acolorado1/dsproject documentation built on Dec. 31, 2020, 6:43 p.m.