Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make recursion optional #150

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Version: 0.2.16.9000
Authors@R: c(
person("Andrie", "de Vries", role=c("aut", "cre", "cph"), email="[email protected]"),
person("Alex", "Chubaty", role="ctb", email="[email protected]"),
person("Alexandre", "Courtiol", role="ctb", email="[email protected]"),
person("Microsoft Corporation", role="cph")
)
License: GPL-2
Expand Down Expand Up @@ -41,7 +42,7 @@ Suggests:
LazyData: true
LazyLoad: true
VignetteBuilder: knitr
RoxygenNote: 7.1.2
RoxygenNote: 7.2.1
Roxygen: list(markdown = TRUE)
Encoding: UTF-8
Language: en-US
Expand Down
10 changes: 8 additions & 2 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
miniCRAN v0.2.17
================

* `makeDepGraph()` and `pkgDep()` gain an argument `recursive` to control whether dependencies of dependencies are retrieved or not (default = TRUE for default behaviour identical to previous versions) (#149)


miniCRAN v0.2.16
================

Expand Down Expand Up @@ -44,7 +50,7 @@ Minor changes:
miniCRAN v0.2.12 (Release date: 2019-07-06)
================

New features:
New features:
* None

Bug fixes:
Expand All @@ -65,7 +71,7 @@ Internal changes
miniCRAN v0.2.11 (Release date: 2018-01-15)
================

New features:
New features:
* None

Bug fixes:
Expand Down
18 changes: 15 additions & 3 deletions R/makeDepGraph.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,13 @@ addDepType <- function(p, type = c("Imports", "Depends", "LinkingTo", "Suggests"
makeDepGraph <- function(
pkg, availPkgs, repos = getOption("repos"), type = "source",
suggests = TRUE, enhances = FALSE,
includeBasePkgs = FALSE, ...)
includeBasePkgs = FALSE,
recursive = TRUE, ...)
{

if (is.character(recursive))
stop("The argument recursive must be a logicial (TRUE or FALSE).")

if (missing(availPkgs)) {
availPkgs <- pkgAvail(repos = repos, type = type)
}
Expand Down Expand Up @@ -84,10 +88,15 @@ makeDepGraph <- function(
))
pkg <- unique(c(p_enh, pkg))
}

pkg2 <- pkg
if(!recursive)
pkg2 <- pkg_orig

p_dep <- unique(unlist(
tools::package_dependencies(
pkg, db = availPkgs,
which = c("Imports", "Depends", "LinkingTo"), recursive = TRUE)
pkg2, db = availPkgs,
which = c("Imports", "Depends", "LinkingTo"), recursive = recursive)
))
pkg <- unique(c(p_dep, pkg))

Expand All @@ -103,6 +112,9 @@ makeDepGraph <- function(
if (nedges && !includeBasePkgs)
edges <- edges[!(edges[["dep"]] %in% basePkgs()), ]

if (nedges && !recursive)
edges <- edges[(edges[["dep"]] %in% pkg), ]

vert <- unique(c(pkg_orig, edges[["dep"]], edges[["package"]]))
ret <- igraph::graph.data.frame(d = edges, directed = TRUE, vertices = vert)
class(ret) <- c("pkgDepGraph", "igraph")
Expand Down
91 changes: 50 additions & 41 deletions R/pkgDep.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ basePkgs <- function()names(which(installed.packages()[, "Priority"] == "base"))

#' Retrieves package dependencies.
#'
#' Performs recursive retrieve for `Depends`, `Imports` and `LinkLibrary`.
#' Performs recursive or non-recursive retrieve for `Depends`, `Imports` and `LinkLibrary`.
#' Performs non-recursive retrieve for `Suggests`.
#'
#'
Expand All @@ -33,37 +33,42 @@ basePkgs <- function()names(which(installed.packages()[, "Priority"] == "base"))
#'
#' @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
#'
#'
#' @param recursive If TRUE, (reverse) dependencies of (reverse) dependencies (and so on) should be included
#'
#' @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, ...)
includeBasePkgs = FALSE, recursive = TRUE,
Rversion = R.version, quiet = FALSE, ...)
{
assert_that(is_package(pkg))

if (is.character(recursive))
stop("The argument recursive must be a logicial (TRUE or FALSE).")

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@") {
Expand All @@ -73,7 +78,7 @@ pkgDep <- function(pkg, availPkgs, repos = getOption("repos"), type = "source",
availPkgs <- pkgAvail(repos = repos, type = type, Rversion = Rversion,
quiet = quiet, ...)
}

assert_that(nrow(availPkgs) > 0, msg = "Unable to retrieve available packages from CRAN")


Expand Down Expand Up @@ -107,9 +112,13 @@ pkgDep <- function(pkg, availPkgs, repos = getOption("repos"), type = "source",
}

# Depends, Imports and LinkingTo
p_dep <- tools::package_dependencies(n_req_all, availPkgs,
pkg2 <- n_req_all
if (!recursive)
pkg2 <- n_req

p_dep <- tools::package_dependencies(pkg2, availPkgs,
which = c("Depends", "Imports", "LinkingTo"),
recursive = TRUE)
recursive = recursive)
n_dep <- unique(unname(unlist(p_dep)))

p_all <- p_dep
Expand Down Expand Up @@ -149,13 +158,13 @@ print.pkgDep <- function(x, ...) {
#' @export
#' @family create repo functions
#' @seealso [pkgDep()]
pkgAvail <- function(repos = getOption("repos"),
type = "source",
pkgAvail <- function(repos = getOption("repos"),
type = "source",
Rversion = R.version, quiet = FALSE) {
assert_that(is_repos(repos))
if (!grepl("^https*://|file:///", repos[1]) && file.exists(repos[1])) {
repos <- paste0("file:///", normalizePath(repos[1],
mustWork = FALSE,
mustWork = FALSE,
winslash = "/"))
} else {
if (!is.null(names(repos)) && isTRUE(unname(repos["CRAN"]) == "@CRAN@")) {
Expand All @@ -165,19 +174,19 @@ pkgAvail <- function(repos = getOption("repos"),
ap <- function() {
if (getRversion() >= "3.3.0") {
utils::available.packages(
contribUrl(repos,
type = type,
Rversion = Rversion),
type = type,
contribUrl(repos,
type = type,
Rversion = Rversion),
type = type,
filters = list(),
repos = repos
)
} else {
utils::available.packages(
contribUrl(repos,
type = type,
Rversion = Rversion),
type = type,
contribUrl(repos,
type = type,
Rversion = Rversion),
type = type,
filters = list()
)
}
Expand All @@ -188,46 +197,46 @@ pkgAvail <- function(repos = getOption("repos"),


# Modified copy of utils::contrib.url()
contribUrl <- function (repos = getOption("repos"),
type = getOption("pkgType"),
contribUrl <- function (repos = getOption("repos"),
type = getOption("pkgType"),
Rversion = R.version) {
Rversion <- twodigitRversion(Rversion)
if (type == "both")
if (type == "both")
type <- "source"
if (type == "binary")
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 ---"),
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))
if (is.null(nm))
nm <- rep("", length(repos))
nm[m] <- "CRAN"
names(repos) <- nm
}
if ("@CRAN@" %in% repos)
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",
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
}
36 changes: 30 additions & 6 deletions inst/examples/example_makeDepGraph.R
Original file line number Diff line number Diff line change
@@ -1,32 +1,56 @@

if (interactive()) {
availPkgs <- cranJuly2014

availPkgs <- pkgAvail(
repos = c(CRAN = "https://cloud.r-project.org"),
type = "source"
)


# Create dependency graph using stored database of available packages
p <- makeDepGraph(
c("ggplot2", "forecast"),
availPkgs = availPkgs
)

if(require(igraph)) plot(p)

# Create dependency graph using newly retrieved database from CRAN

p <- makeDepGraph(
c("ggplot2", "forecast"),
repos = c(CRAN = getOption("minicran.mran")),
type = "source"
)

if(require(igraph)) plot(p)

# Same thing without suggests

p <- makeDepGraph(
c("ggplot2", "forecast"),
repos = c(CRAN = getOption("minicran.mran")),
type = "source",
suggests = FALSE
)

if(require(igraph)) plot(p)


# Same thing without suggests and recursive dependencies

p <- makeDepGraph(
c("ggplot2", "forecast"),
repos = c(CRAN = getOption("minicran.mran")),
type = "source",
suggests = FALSE,
recursive = FALSE
)

if(requireNamespace("igraph", quietly = TRUE)) {
plot(p)
} else {
message("install package `igraph` to view dependency graph")
}

}
Loading