-
Notifications
You must be signed in to change notification settings - Fork 2
/
packload.r
85 lines (83 loc) · 2.92 KB
/
packload.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
##### packload function for installing, updating, and loading packages #####
## Last updated 21 October 2020 by J.D. Muehlbauer
## Function checks if a package is currently installed on machine.
## If not, it installs it (via CRAN or else jmuehlbauer-usgs/R-packages).
## Also has an updater function to update the packages if requested.
## Loads all packages fed to the function too.
## The quiet argument does all this as quietly as possible.
packload <- function(packages, updater = FALSE, quiet = TRUE){
## Set system environment and download options for GitHub installs to work
Sys.setenv(TAR = 'internal')
options(download.file.method = "wininet")
## Check for currently installed packages
packs <- vector()
mypacks <- rownames(installed.packages())
for(i in 1 : length(packages)){
if(!(packages[i] %in% mypacks)){
packs <- c(packs, packages[i])
}
}
## Specify CRAN mirror if not already done
if(is.null(getOption('repos'))){
repos <- "https://cran.cnr.berkeley.edu/"
} else{
repos <- getOption('repos')
}
## Install new packages
if(length(packs) > 0){
avl <- rownames(available.packages())
for(i in 1 : length(packs)){
## Install from CRAN
if(packs[i] %in% avl){
install.packages(packs[i], quiet = quiet, repos = repos)
## Install from GitHub jmuehlbauer/R-packages repository
} else{
## Make sure devtools is installed first
if(!('devtools' %in% mypacks)){
install.packages('devtools', quiet = quiet)
}
require(devtools)
repo <- 'jmuehlbauer-usgs/R-packages'
tryit <- try(suppressWarnings(install_github(repo = repo,
subdir = packs[i], quiet = quiet)), silent = TRUE)
}
}
}
## Update packages if requested. Performs many of the same tasks as above
if(updater == TRUE){
packs2 = packages[packages %in% packs == FALSE]
if(exists('avl') == FALSE){
avl <- rownames(available.packages())
}
for(i in 1 : length(packs2)){
if(packs2[i] %in% avl){
update.packages(packs2[i], quiet = quiet, repos = repos)
} else{
if(!('devtools' %in% rownames(installed.packages()))){
install.packages('devtools', quiet = quiet)
} else{
update.packages('devtools', quiet = quiet)
}
require(devtools)
repo <- 'jmuehlbauer-usgs/R-packages'
tryit <- try(suppressWarnings(install_github(repo = repo,
subdir = packs2[i], quiet = quiet, force = TRUE)), silent = TRUE)
}
}
}
## See if any packages are already loaded
unloaded <- packages[!packages %in% (.packages())]
## Load unloaded packages (or all packages if updater = TRUE)
if(updater == TRUE){
loaded <- lapply(packages, require, quietly = quiet,
warn.conflicts = !quiet, character.only = TRUE)
} else {
if(quiet == TRUE){
loaded <- suppressWarnings(lapply(unloaded, require, quietly = quiet,
warn.conflicts = !quiet, character.only = TRUE))
} else{
loaded <- lapply(unloaded, require, quietly = quiet,
warn.conflicts = !quiet, character.only = TRUE)
}
}
}