File: lib.R

package info (click to toggle)
r-cran-bbmisc 1.13.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,256 kB
  • sloc: ansic: 176; sh: 9; makefile: 5
file content (28 lines) | stat: -rw-r--r-- 817 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#' @title A wrapper for \code{library}
#'
#' @description
#' Tries to load packages. If the packages are not found, they will be installed from
#' the default repository. This function is intended for use in interactive sessions
#' and should not be used by other packages.
#'
#' @param ... [any]\cr
#'   Package names.
#' @return [\code{logical}]: Named logical vector determining the success
#'   of package load.
#' @export
#' @examples \dontrun{
#' lib("BBmisc", "MASS", "rpart")
#' }
lib = function(...) {
  getLib = function(pkg) {
    ok = suppressWarnings(require(pkg, character.only = TRUE))
    if (!ok && !is.error(try(install.packages(pkg)))) {
      ok = require(pkg, character.only = TRUE)
    }
    ok
  }

  pkgs = unique(c(...))
  assertCharacter(pkgs, any.missing = FALSE)
  vlapply(pkgs, getLib)
}