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

expose convert_edgelist (#56) #74

Merged
merged 4 commits into from
Feb 23, 2023
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: 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)))
})