-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.R
63 lines (58 loc) · 2.19 KB
/
utils.R
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Utility R functions needed by the Mussel Modeling toolbox.
#' @title Install and load needed packages
#'
#' @description Tests if packages are installed and if not installs them. Once
#' packages are installed it loads them.
#'
#' @export
#' @param need_pkgs A character vector of package names.
#'
#' @return Installs and loads the requested packages.
#'
#' @details Replaces the `pacman::p_load` function that requires the latest R
#' version. This function uses only base R functions. This function only
#' installs packages from the currently set repositories (e.g., CRAN,
#' CRANextra).
#'
load_packages <- function(need_pkgs) {
# Determine the uninstalled packages from need_pkgs
uninst_pkgs <- need_pkgs[!(need_pkgs %in% installed.packages()[, "Package"])]
# Install uninstalled packages
if (length(uninst_pkgs)) install.packages(uninst_pkgs, dependencies = TRUE)
# Load all needed packages
lapply(need_pkgs, require, character.only = TRUE)
}
load_resri <- function() {
# Install `resri` from github
if (!require("resri")) {
devtools::install_github(repo = "mpdougherty/resri",
force = TRUE,
upgrade = TRUE,
dependencies = TRUE,
options(install.packages.check.source = "no"))
if ("resri" %in% rownames(installed.packages()) == TRUE) {
message("The `resri` package was installed.")
}
} else {
if ("resri" %in% rownames(installed.packages()) == TRUE) {
message("The `resri` package was already installed.")
}
}
}
load_mcat <- function() {
# Install `mcat` from github
if (!require("mcat")) {
devtools::install_github(repo = "mpdougherty/mcat",
force = TRUE,
upgrade = TRUE,
dependencies = TRUE,
options(install.packages.check.source = "no"))
if ("resri" %in% rownames(installed.packages()) == TRUE) {
message("The `mcat` package was installed.")
}
} else {
if ("mcat" %in% rownames(installed.packages()) == TRUE) {
message("The `mcat` package was already installed.")
}
}
}