-
Notifications
You must be signed in to change notification settings - Fork 1
/
fun_def_h_load.R
38 lines (27 loc) · 1.42 KB
/
fun_def_h_load.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
#function to check if a pkg is installed or not, if not it installs it and either way it's loaded.
#inspired by pacman::p_load()
h_load <- function(pkg, verbose = F, version = NULL, repos = "http://cran.us.r-project.org"){
# p <- "ggpubr"
# pkg <- c("geiger", "ape")
for(p in pkg){
if(is.null(version) & (!(p %in% rownames(installed.packages())))){ #if no version is specified, check if it's installed and if not then go and install it as normal
install.packages(p, dependencies = T, repos = repos)
if(verbose == T){
cat(paste0("Installed ", p, ".\n"))}
}
if(!is.null(version) & (!(p %in% rownames(installed.packages())))){
if(!"devtools"%in% rownames(installed.packages())){ #install devtools if it isn't already installed
install.packages(devtools, dependencies = T, repos = repos)
}
library(devtools, quietly = T, verbose = F, warn.conflicts = F)
devtools::install_version(p, version = version, dependencies = T, repos = repos)
if(verbose == T){
cat(paste0("Installed ", p, ", version ", version, ".\n"))}
}
if(verbose == T){
library(p, character.only = T, quietly = F)
cat(paste0("Loaded ", p, ", version ",packageVersion(p),".\n"))
}else{
suppressMessages(library(p, character.only = T, quietly = T, verbose = F, warn.conflicts = F))}
}
}