Skip to content

Commit

Permalink
Merge pull request #74 from schochastics/edgelist
Browse files Browse the repository at this point in the history
expose convert_edgelist (#56)
  • Loading branch information
chainsawriot authored Feb 23, 2023
2 parents fabb92d + e2f7cb4 commit 6739512
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 11 deletions.
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

S3method(as_pkgrefs,default)
S3method(as_pkgrefs,sessionInfo)
S3method(convert_edgelist,default)
S3method(convert_edgelist,rang)
S3method(convert_edgelist,ranglet)
S3method(print,rang)
S3method(print,ranglet)
export(as_pkgrefs)
export(convert_edgelist)
export(dockerise)
export(dockerise_rang)
export(dockerize)
Expand Down
51 changes: 51 additions & 0 deletions R/edgelist.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#' Convert Data Structures to rang edgelist
#'
#' This generic function converts several data structures provided by rang into an edgelist of package dependencies.
#' @param x, supported data structures are `rang` and `ranglet` S3 objects
#' @param ..., not used
#' @return a data frame of directed edges of dependencies
#' @details the resulting data frame can be converted to an igraph object for plotting and analysis via the function [igraph::graph_from_data_frame()]
#' @export
#' @examples
#' \donttest{
#' if (interactive()) {
#' graph <- resolve(pkgs = c("openNLP", "LDAvis", "topicmodels", "quanteda"),
#' snapshot_date = "2020-01-16")
#'
#' # dependency edgelist of a single package
#' convert_edgelist(graph$ranglets[[1]])
#'
#' # full dependency edgelist
#' convert_edgelist(graph)
#' }
#' }
convert_edgelist <- function(x, ...) {
UseMethod("convert_edgelist", x)
}

#' @rdname convert_edgelist
#' @export
convert_edgelist.default <- function(x, ...){
stop(paste("don't know how to convert an object of type",class(x),"to a rang edgelist"), call. = FALSE)
}

#' @rdname convert_edgelist
#' @export
convert_edgelist.ranglet <- function(x, ...){
output <- data.frame(from = x$pkgref, to = .extract_queryable_dependencies(x$original, x$no_enhances, x$no_suggests))
for (dep in x$deps) {
if (!.is_terminal_node(dep, x$no_enhances)) {
el <- data.frame(from = unique(dep$x_pkgref), to = .extract_queryable_dependencies(dep, x$no_enhances, x$no_suggests))
output <- rbind(output, el)
}
}
output
}

#' @rdname convert_edgelist
#' @export
convert_edgelist.rang <- function(x, ...){
el <- do.call("rbind",lapply(x$ranglets,convert_edgelist))
rownames(el) <- NULL
el
}
11 changes: 0 additions & 11 deletions R/resolve.R
Original file line number Diff line number Diff line change
Expand Up @@ -366,17 +366,6 @@ print.rang <- function(x, all_pkgs = FALSE, ...) {
}
}

convert_edgelist <- function(x) {
output <- data.frame(x = x$pkg, y = .extract_queryable_dependencies(x$original, x$no_enhances, x$no_suggests))
for (dep in x$deps) {
if (!.is_terminal_node(dep, x$no_enhances)) {
el <- data.frame(x = unique(dep$x), y = .extract_queryable_dependencies(dep, x$no_enhances, x$no_suggests))
output <- rbind(output, el)
}
}
output
}

## extract all the pkgrefs of deps and pkgs: for .sysreqs
.extract_pkgrefs <- function(rang) {
original_pkgrefs <- names(rang$ranglets)
Expand Down
45 changes: 45 additions & 0 deletions man/convert_edgelist.Rd

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

19 changes: 19 additions & 0 deletions tests/testthat/test-edgelist.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
test_that("convert ranglet to edgelist", {
graph <- readRDS("../testdata/graph.RDS")
el <- convert_edgelist(graph$ranglets[[1]])
expect_s3_class(el,"data.frame")
expect_equal(nrow(el),4L)
})

test_that("convert rang to edgelist", {
graph <- readRDS("../testdata/graph.RDS")
el <- convert_edgelist(graph)
expect_s3_class(el,"data.frame")
expect_equal(nrow(el),121L)
})

test_that("convert edgelist error", {
expect_error(convert_edgelist("abc"))
expect_error(convert_edgelist(42))
expect_error(convert_edgelist(cbind(1:5,6:10)))
})

0 comments on commit 6739512

Please sign in to comment.