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

dependencies arg for package_info() #22

Merged
merged 2 commits into from
Sep 25, 2018
Merged
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
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: sessioninfo
Title: R Session Information
Version: 1.0.1.9000
Version: 1.0.1.9001
Author: Gábor Csárdi, R core, Hadley Wickham, Winston Chang,
Robert M Flight, Kirill Müller, Jim Hester
Maintainer: Gábor Csárdi <csardi.gabor@gmail.com>
Expand All @@ -11,7 +11,7 @@ License: GPL-2
LazyData: true
URL: https://github.com/r-lib/sessioninfo#readme
BugReports: https://github.com/r-lib/sessioninfo/issues
RoxygenNote: 6.0.1.9000
RoxygenNote: 6.1.0
Roxygen: list(markdown = TRUE)
Suggests:
callr,
Expand Down
42 changes: 26 additions & 16 deletions R/dependent-packages.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

dependent_packages <- function(pkgs) {
pkgs <- find_deps(pkgs, utils::installed.packages(), top_dep = NA)
dependent_packages <- function(pkgs, dependencies) {

ideps <- interpret_dependencies(dependencies)

pkgs <- find_deps(pkgs, utils::installed.packages(), ideps[[1]], ideps[[2]])
desc <- lapply(pkgs, utils::packageDescription)

loaded_pkgs <- pkgs %in% setdiff(loadedNamespaces(), "base")
Expand Down Expand Up @@ -31,13 +34,11 @@ pkg_path_disk <- function(desc) {
}

find_deps <- function(pkgs, available = utils::available.packages(),
top_dep = TRUE, rec_dep = NA, include_pkgs = TRUE) {
top_dep = c(dep_types_hard(), "Suggests"),
rec_dep = dep_types_hard(), include_pkgs = TRUE) {

if (length(pkgs) == 0 || identical(top_dep, FALSE)) return(character())

top_dep <- standardise_dep(top_dep)
rec_dep <- standardise_dep(rec_dep)

if (length(top_dep) > 0) {
top <- tools::package_dependencies(pkgs, db = available, which = top_dep)
top_flat <- unlist(top, use.names = FALSE)
Expand All @@ -60,16 +61,25 @@ find_deps <- function(pkgs, available = utils::available.packages(),
unique(c(if (include_pkgs) pkgs, top_flat, rec_flat))
}

standardise_dep <- function(x) {
if (identical(x, NA)) {
c("Depends", "Imports", "LinkingTo")
} else if (isTRUE(x)) {
c("Depends", "Imports", "LinkingTo", "Suggests")
} else if (identical(x, FALSE)) {
character(0)
} else if (is.character(x)) {
x
dep_types_hard <- function() c("Depends", "Imports", "LinkingTo")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason this needs to be a function rather than just a constant?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just trying to avoid non-function code in packages in general. Got bitten many times. But yeah, it could be a constant this time.

dep_types_soft <- function() c("Suggests", "Enhances")
dep_types <- function() c(dep_types_hard(), dep_types_soft())

is_na_scalar <- function(x) length(x) == 1 && is.na(x)

interpret_dependencies <- function(dp) {
hard <- dep_types_hard()

if (isTRUE(dp)) {
list(c(hard, "Suggests"), hard)

} else if (identical(dp, FALSE)) {
list(character(), character())

} else if (is_na_scalar(dp)) {
list(hard, hard)

} else {
stop("Dependencies must be a boolean or a character vector", call. = FALSE)
list(dp, dp)
}
}
7 changes: 5 additions & 2 deletions R/package-info.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#' all dependencies of the package.
#' @param include_base Include base packages in summary? By default this is
#' false since base packages should always match the R version.
#' @param dependencies Whether to include the (recursive) dependencies
#' as well. See the `dependencies` argument of [utils::install.packages()].
#' @return A data frame with columns:
#' * `package`: package name.
#' * `ondiskversion`: package version (on the disk, which is sometimes
Expand Down Expand Up @@ -36,12 +38,13 @@
#' package_info()
#' package_info("sessioninfo")

package_info <- function(pkgs = NULL, include_base = FALSE) {
package_info <- function(pkgs = NULL, include_base = FALSE,
dependencies = NA) {

if (is.null(pkgs)) {
pkgs <- loaded_packages()
} else {
pkgs <- dependent_packages(pkgs)
pkgs <- dependent_packages(pkgs, dependencies)
}

desc <- lapply(pkgs$package, utils::packageDescription, lib.loc = .libPaths())
Expand Down
5 changes: 4 additions & 1 deletion man/package_info.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 1 addition & 23 deletions tests/testthat/test-dependent-packages.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ test_that("dependent_packages", {
)

exp <- dep[, setdiff(colnames(dep), c("path", "loadedpath"))]
tec <- dependent_packages("devtools")
tec <- dependent_packages("devtools", NA)
tec <- tec[, setdiff(colnames(tec), c("path", "loadedpath"))]
expect_equal(exp, tec)
})
Expand Down Expand Up @@ -73,25 +73,3 @@ test_that("find_deps", {
"foobar"
)
})

test_that("standardise_dep", {

cases <- list(
NA,
TRUE,
FALSE,
"Imports"
)

for (c in cases) {
expect_true(
all(standardise_dep(c) %in%
c("Depends", "Imports", "Suggests", "LinkingTo"))
)
}

expect_error(
standardise_dep(list(1,2,3)),
"Dependencies must be a boolean or a character"
)
})