Skip to content

Commit

Permalink
Add getDependencyData()
Browse files Browse the repository at this point in the history
  • Loading branch information
hsonne committed May 3, 2024
1 parent 9556248 commit ec4b3dc
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export(equidistantAngles)
export(exampleLinksAndNodes)
export(getAuthors)
export(getCranPackageDatabase)
export(getDependencyData)
export(getPackageFilesToInstall)
export(getPackageLicences)
export(getRVersionMajorMinor)
Expand Down Expand Up @@ -54,6 +55,7 @@ importFrom(kwb.utils,defaultIfNA)
importFrom(kwb.utils,defaultIfNULL)
importFrom(kwb.utils,excludeNULL)
importFrom(kwb.utils,extractSubstring)
importFrom(kwb.utils,fullySorted)
importFrom(kwb.utils,getAttribute)
importFrom(kwb.utils,get_homedir)
importFrom(kwb.utils,hsRestoreAttributes)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ The following public functions were added:

- getAuthors()
- getCranPackageDatabase()
- getDependencyData()
- getPackageLicences()
- hasGplLicence()
- packageDependenciesByType()
Expand Down
72 changes: 72 additions & 0 deletions R/getDependencyData.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# getDependencyData ------------------------------------------------------------

#' Get Package Dependency Data from Package Database
#'
#' @param db package data base as e.g. returned by
#' \code{\link{getCranPackageDatabase}}
#' @param fields types of dependencies to be considered. Default:
#' \code{c("Depends", "Imports", "LinkingTo", "Suggests", "Enhances")}
#' @export
getDependencyData <- function(
db,
fields = c("Depends", "Imports", "LinkingTo", "Suggests", "Enhances")
)
{
stopifnot(`db must be a matrix or a data frame` = length(dim(db)) == 2L)

db <- as.data.frame(db)
packages <- selectColumns(db, "Package")

deps <- lapply(fields, function(field) {
catAndRun(
sprintf("Analysing %s field", field),
packages %>%
toDependencyData(selectColumns(db, field)) %>%
cbind(
type = tolower(field),
reverse = FALSE
)
)
}) %>%
rbindAll() %>%
fullySorted() %>%
moveColumnsToFront(c("package", "type"))
}

# toDependencyData -------------------------------------------------------------
toDependencyData <- function(packages, strings)
{
stopifnot(length(packages) == length(strings))

# Split dependency strings at comma (after removing new line or space)
parts <- strsplit(gsub("\n|\\s+", "", strings), ",")

data.frame(
package = rep(packages, lengths(parts)),
splitDependency(unlist(parts))
)
}

# splitDependency --------------------------------------------------------------
splitDependency <- function(x)
{
stopifnot(`No spaces allowed!` = !any(grepl("\\s", x)))

pattern <- "^([^(]+)(\\(([<>=]+)(.*)\\))?"
matches <- is.na(x) | grepl(pattern, x)

if (any(!matches)) {
stopFormatted(
"Non-matching dependency string: '%s'", x[!matches][1L]
)
}

cbind(
dep_full = x,
extractSubstring(pattern, x, index = c(
dep_name = 1L,
dep_version_op = 3L,
dep_version_no = 4L
))
)
}
4 changes: 4 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ excludeNull <- kwb.utils::excludeNULL
#' @importFrom kwb.utils extractSubstring
extractSubstring <- kwb.utils::extractSubstring

# fullySorted ------------------------------------------------------------------
#' @importFrom kwb.utils fullySorted
fullySorted <- kwb.utils::fullySorted

# getAttribute -----------------------------------------------------------------
#' @importFrom kwb.utils getAttribute
getAttribute <- kwb.utils::getAttribute
Expand Down
21 changes: 21 additions & 0 deletions man/getDependencyData.Rd

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

0 comments on commit ec4b3dc

Please sign in to comment.