-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from schochastics/edgelist
expose convert_edgelist (#56)
- Loading branch information
Showing
5 changed files
with
119 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) | ||
}) |