-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathpkgDep.R
243 lines (222 loc) · 7.34 KB
/
pkgDep.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#' Returns names of base packages.
#'
#' Retrieves names of installed packages by calling
#' [utils::installed.packages()] and returning only those packages where
#' `Priority == "base"`.
#'
#' @export
#' @family dependency functions
#'
#' @seealso [pkgDep()]
basePkgs <- function() names(which(installed.packages()[, "Priority"] == "base"))
availPkgNames <- function(pdb) {
pdb[, "Package"]
}
#' Retrieves package dependencies.
#'
#' Performs recursive retrieve for `Depends`, `Imports` and `LinkLibrary`.
#' Performs non-recursive retrieve for `Suggests`.
#'
#'
#' @param pkg Character vector of packages.
#'
#' @param availPkgs Data frame with an element called `package`. The `package`
#' element is a vector of available packages. Defaults to reading this list
#' from CRAN, using [available.packages()]
#'
#' @param repos URL(s) of the 'contrib' sections of the repositories, e.g.
#' `"https://cran.us.r-project.org"`. Passed to [available.packages()]
#'
#' @param type Possible values are (currently) "source", "mac.binary" and
#' "win.binary": the binary types can be listed and downloaded but not
#' installed on other platforms. Passed to [download.packages()].
#'
#' @param depends If TRUE, retrieves `Depends`, `Imports` and `LinkingTo` dependencies
#' (non-recursively)
#'
#' @param suggests If TRUE, retrieves Suggests dependencies (non-recursively)
#'
#' @param enhances If TRUE, retrieves Enhances dependencies (non-recursively)
#'
#' @param quiet If TRUE, suppresses warnings
#'
#' @param includeBasePkgs If TRUE, include base R packages in results
#'
#' @template Rversion
#'
#' @param ... Other arguments passed to [available.packages()]
#'
#' @export
#' @family dependency functions
#'
#' @return character vector of package names
#'
#' @example /inst/examples/example_pkgDep.R
#'
pkgDep <- function(pkg, availPkgs, repos = getOption("repos"), type = "source",
depends = TRUE, suggests = TRUE, enhances = FALSE,
includeBasePkgs = FALSE, Rversion = R.version, quiet = FALSE, ...)
{
assert_that(is_package(pkg))
if (!depends & !suggests & !enhances) {
warning("Returning nothing, since depends, suggests and enhances are all FALSE")
return(character(0))
}
if (missing(availPkgs)) {
if (!is.null(names(repos)) & repos["CRAN"] == "@CRAN@") {
repos <- p3m()
}
if (is.na(type)) type <- "source"
availPkgs <- pkgAvail(repos = repos, type = type, Rversion = Rversion,
quiet = quiet, ...)
}
assert_that(
nrow(availPkgs) > 0,
msg = sprintf("Unable to retrieve %s package %s from %s", type, pkg, repos)
)
pkgInAvail <- pkg %in% availPkgs[, "Package"]
pkgInAvail <- pkg %in% availPkgNames(availPkgs)
if (sum(pkgInAvail) == 0 ) stop("No valid packages in pkg")
if (sum(pkgInAvail) < length(pkg)) {
warning("Package not recognized: ", paste(pkg[!pkgInAvail], collapse = ", "))
}
n_req <- pkg[pkgInAvail]
n_req_all <- pkg
# Suggests
if (suggests) {
p_sug <- tools::package_dependencies(n_req, availPkgs,
which = "Suggests", recursive = FALSE)
n_sug <- unique(unname(unlist(p_sug)))
n_req_all <- c(n_req_all, n_sug)
} else {
p_sug <- NA
}
# Enhances
if (enhances) {
p_enh <- tools::package_dependencies(n_req, availPkgs,
which = "Enhances", recursive = FALSE)
n_enh <- unique(unname(unlist(p_enh)))
n_req_all <- c(n_req_all, n_enh)
} else {
p_enh <- NA
}
# Depends, Imports and LinkingTo
p_dep <- tools::package_dependencies(n_req_all, availPkgs,
which = c("Depends", "Imports", "LinkingTo"),
recursive = TRUE)
n_dep <- unique(unname(unlist(p_dep)))
p_all <- p_dep
n_all <- unique(c(n_dep, n_req_all))
n_all <- c(n_req, setdiff(n_all, n_req))
ret <- n_all
if(!includeBasePkgs) ret <- ret[!ret %in% basePkgs()]
attr(ret, "pkgs") <- list(
n_req = n_req,
n_all = n_all,
p_dep = p_dep,
p_sug = p_sug,
p_enh = p_enh,
p_all = p_all
)
class(ret) <- c("pkgDep", "character")
assert_that(is_package_vector(ret))
ret
}
#' @export
print.pkgDep <- function(x, ...) {
attr(x, "pkgs") <- NULL
class(x) <- "character"
print(as.vector(x), ...)
}
#' Reads available packages from CRAN repository.
#'
#' This is a thin wrapper around [utils::available.packages()]. If the argument
#' `path` is supplied, then the function attempts to read from a local
#' repository, otherwise attempts to read from a CRAN mirror at the `repos` url.
#'
#' @inheritParams pkgDep
#'
#' @param filters passed to [utils::available.packages]
#'
#' @export
#' @family create repo functions
#' @seealso [pkgDep()]
pkgAvail <- function(repos = getOption("repos"),
type = "source",
Rversion = R.version, quiet = FALSE,
filters = NULL) {
assert_that(is_repos(repos))
if (!grepl("^https*://|file:///", repos[1]) && file.exists(repos[1])) {
repos <- paste0("file:///", normalizePath(repos[1],
mustWork = FALSE,
winslash = "/"))
} else {
if (!is.null(names(repos)) && isTRUE(unname(repos["CRAN"]) == "@CRAN@")) {
repos <- p3m()
}
}
ap <- function() {
if (getRversion() >= "3.3.0") {
utils::available.packages(
contribUrl(repos,
type = type,
Rversion = Rversion),
type = type,
filters = filters,
repos = repos
)
} else {
utils::available.packages(
contribUrl(repos,
type = type,
Rversion = Rversion),
type = type,
filters = filters
)
}
}
if (quiet) suppressWarnings(ap()) else ap()
}
# Modified copy of utils::contrib.url()
contribUrl <- function (repos = getOption("repos"),
type = getOption("pkgType"),
Rversion = R.version) {
Rversion <- twodigitRversion(Rversion)
if (type == "both")
type <- "source"
if (type == "binary")
type <- .Platform$pkgType
if (is.null(repos)) return(NULL)
if ("@CRAN@" %in% repos && interactive()) {
cat(gettext("--- Please select a CRAN mirror for use in this session ---"),
"\n", sep = "")
flush.console()
chooseCRANmirror()
m <- match("@CRAN@", repos)
nm <- names(repos)
repos[m] <- getOption("repos")["CRAN"]
if (is.null(nm))
nm <- rep("", length(repos))
nm[m] <- "CRAN"
names(repos) <- nm
}
if ("@CRAN@" %in% repos)
stop("trying to use CRAN without setting a mirror")
ver <- Rversion
mac.path <- "macosx"
if (substr(type, 1L, 11L) == "mac.binary.") {
mac.path <- paste(mac.path, substring(type, 12L), sep = "/")
type <- "mac.binary"
}
res <- switch(type,
source = paste(gsub("/$", "", repos),
"src", "contrib",
sep = "/"),
mac.binary = paste(gsub("/$", "", repos),
"bin", mac.path, "contrib",
ver, sep = "/"),
win.binary = paste(gsub("/$", "", repos),
"bin", "windows", "contrib",
ver, sep = "/"))
res
}