-
Notifications
You must be signed in to change notification settings - Fork 1
/
packages.R
84 lines (79 loc) · 2.55 KB
/
packages.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
#' Build the pkg dependency graph
#'
#' Uses \pkg{igraph} and `reproducible::pkgDep`.
#'
#' @param pkgs A character vector of package names. Default is
#' `c("LandR", "pemisc", "map", "SpaDES", "SpaDES.tools", "SpaDES.core", "SpaDES.addins", "SpaDES.shiny", "reproducible", "quickPlot")`
#'
#' @param plot.it Logical. If `TRUE`, it will plot the `igraph`.
#'
#' @importFrom data.table data.table rbindlist
#' @importFrom graphics plot
#' @importFrom reproducible pkgDep
#' @return
#' A list of 2: `dt` a data.table of the dependencies, and `dtGraph`
#' an `igraph` object that can be plotted with `plot()`
pkgDepsGraph <- function(pkgs = c("LandR", "pemisc", "map", "SpaDES",
"SpaDES.tools", "SpaDES.core",
"SpaDES.addins", "SpaDES.shiny",
"reproducible", "quickPlot"),
plot.it = TRUE) {
if (reproducible::.requireNamespace("igraph")) {
dt <- lapply(pkgs, function(pkg) {
deps <- pkgs[pkgs %in% pkgDep(pkg)[[1]]]
if (NROW(deps))
data.table(pkg = pkg,
depends = deps)
})
dt <- rbindlist(dt);
dtGraph <- igraph::graph_from_data_frame(dt);
if (isTRUE(plot.it))
plot(dtGraph)
return(list(dt = dt, dtGraph = dtGraph))
}
}
#' Check whether a package is one installed from GitHub
#'
#' Determines whether a string that may correspond to a package name
#' (e.g., `repo/package@branch`), could be a package installed from GitHub.
#' This is determined solely by the presence of a `/` in the string.
#' See example below.
#'
#' @param x character vector of package names
#'
#' @return a named logical vector
#'
#' @export
#'
#' @examples
#' pkgs <- c("dplyr", "PredictiveEcology/pemisc", "PredictiveEcology/SpaDES.core@development")
#' isGitHubPkg(pkgs) ## FALSE TRUE TRUE
isGitHubPkg <- Vectorize(function(x) {
if (length(strsplit(x, "/")[[1]]) == 1) FALSE else TRUE
})
#' Get the package name from a GitHub `repo/package@branch` string
#'
#' @param x character vector of package names
#'
#' @return a named character vector
#'
#' @export
#'
#' @examples
#' pkgs <- c("dplyr", "PredictiveEcology/pemisc", "PredictiveEcology/SpaDES.core@development")
#' ghPkgName(pkgs) ## "dplyr" "pemisc" "SpaDES.core"
ghPkgName <- Vectorize(function(x) {
y <- strsplit(x, "/")[[1]]
if (length(y) == 1) {
repo <- NULL
pkg <- y
branch <- NULL
} else {
z <- strsplit(y[2], "@")[[1]]
repo <- y[1]
pkg <- z[1]
branch <- z[2]
if (is.na(branch)) branch <- "master"
}
return(pkg)
})