Skip to content

feat!: Accept numeric edge IDs #758

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
117 changes: 63 additions & 54 deletions R/data_frame.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,17 @@ graph.data.frame <- function(d, directed = TRUE, vertices = NULL) { # nocov star
#' is returned, in a list with named entries `vertices` and `edges`.
#'
#' @param d A data frame containing a symbolic edge list in the first two
#' columns. Additional columns are considered as edge attributes. Since
#' columns, as vertex names or vertex IDs.
# Additional columns are considered as edge attributes. Since
#' version 0.7 this argument is coerced to a data frame with
#' `as.data.frame`.
#' [as.data.frame()].
#' @param directed Logical scalar, whether or not to create a directed graph.
#' @param vertices A data frame with vertex metadata, or `NULL`. See
#' details below. Since version 0.7 this argument is coerced to a data frame
#' with `as.data.frame`, if not `NULL`.
#' with [as.data.frame()], if not `NULL`.
#' @return An igraph graph object for `graph_from_data_frame()`, and either a
#' data frame or a list of two data frames named `edges` and
#' `vertices` for `as.data.frame`.
#' `vertices` for [as.data.frame()].
#' @note For `graph_from_data_frame()` `NA` elements in the first two
#' columns \sQuote{d} are replaced by the string \dQuote{NA} before creating
#' the graph. This means that all `NA`s will correspond to a single
Expand Down Expand Up @@ -155,82 +156,90 @@ graph.data.frame <- function(d, directed = TRUE, vertices = NULL) { # nocov star
#' @export
graph_from_data_frame <- function(d, directed = TRUE, vertices = NULL) {
d <- as.data.frame(d)
if (!is.null(vertices)) {
if (is.character(vertices) || is.factor(vertices)) {
vertices <- data.frame(name = as.character(vertices))
} else if (is.matrix(vertices)) {
vertices <- as.data.frame(vertices)
} else if (!is.null(vertices) && !is.data.frame(vertices)) {
stop("`vertices` must be a data frame or a character vector if given")
}

if (ncol(d) < 2) {
stop("the data frame should contain at least two columns")
stop("`d` should contain at least two columns")
}

## Handle if some elements are 'NA'
if (any(is.na(d[, 1:2]))) {
warning("In `d' `NA' elements were replaced with string \"NA\"")
d[, 1:2][is.na(d[, 1:2])] <- "NA"
}
if (!is.null(vertices) && any(is.na(vertices[, 1]))) {
warning("In `vertices[,1]' `NA' elements were replaced with string \"NA\"")
vertices[, 1][is.na(vertices[, 1])] <- "NA"
if (!is.null(vertices) && ncol(vertices) >= 1) {
names <- vertices$name
if (!is.null(names)) {
if (anyNA(names)) {
warning('Replacing `NA` in vertex names in `vertices` with the string "NA"')
names[is.na(names)] <- "NA"
}
if (anyDuplicated(names)) {
stop("Duplicate vertex names")
}
}
} else {
names <- NULL
}

names <- unique(c(as.character(d[, 1]), as.character(d[, 2])))
if (!is.null(vertices)) {
names2 <- names
vertices <- as.data.frame(vertices)
if (ncol(vertices) < 1) {
stop("Vertex data frame contains no rows")
if (is.numeric(d[[1]]) && !is.factor(d[[1]]) && is.numeric(d[[2]]) && !is.factor(d[[2]])) {
edges <- rbind(d[[1]], d[[2]])
names <- seq_len(max(edges, 0L))
} else {
if (is.null(names)) {
names <- unique(c(as.character(d[[1]]), as.character(d[[2]])))
}

if (is.null(vertices)) {
vertices <- data.frame(name = names)
} else if (!("name" %in% names(vertices))) {
vertices <- cbind(data.frame(name = names), vertices)
}
names <- as.character(vertices[, 1])
if (any(duplicated(names))) {
stop("Duplicate vertex names")

name_edges <- rbind(as.character(d[[1]]), as.character(d[[2]]))

if (anyNA(name_edges)) {
warning('Replacing `NA` in vertex names in `d` with the string "NA"')
name_edges[is.na(name_edges)] <- "NA"
}
if (any(!names2 %in% names)) {
stop("Some vertex names in edge list are not listed in vertex data frame")

edges <- matrix(match(name_edges, names), nrow = 2)
if (anyNA(edges)) {
stop(
"Vertex name ",
name_edges[is.na(edges)][[1]],
" in edge list is not listed in vertex data frame"
)
}
}

# create graph
g <- make_empty_graph(n = 0, directed = directed)

# vertex attributes
attrs <- list(name = names)
if (!is.null(vertices)) {
if (ncol(vertices) > 1) {
for (i in 2:ncol(vertices)) {
newval <- vertices[, i]
if (inherits(newval, "factor")) {
newval <- as.character(newval)
}
attrs[[names(vertices)[i]]] <- newval
}
}
}
vattrs <- lapply(vertices, unfactor)

# add vertices
g <- add_vertices(g, length(names), attr = attrs)

# create edge list
from <- as.character(d[, 1])
to <- as.character(d[, 2])
edges <- rbind(match(from, names), match(to, names))
g <- add_vertices(g, length(names), attr = vattrs)

# edge attributes
attrs <- list()
if (ncol(d) > 2) {
for (i in 3:ncol(d)) {
newval <- d[, i]
if (inherits(newval, "factor")) {
newval <- as.character(newval)
}
attrs[[names(d)[i]]] <- newval
}
}
eattrs <- lapply(d[-1:-2], unfactor)

# add the edges
g <- add_edges(g, edges, attr = attrs)
g <- add_edges(g, edges, attr = eattrs)

g
}

unfactor <- function(x) {
if (!inherits(x, "factor")) {
return(x)
}

as.character(x)
}

#' @rdname graph_from_data_frame
#' @param ... Passed to `graph_from_data_frame()`.
#' @export
Expand Down
6 changes: 3 additions & 3 deletions man/graph.data.frame.Rd

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

8 changes: 4 additions & 4 deletions man/graph_from_data_frame.Rd

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

63 changes: 57 additions & 6 deletions revdep/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,61 @@
# Revdeps

## Failed to check (3)
## New problems (54)

|package |version |error |warning |note |
|:---------|:-------|:-----|:-------|:----|
|multinma |0.6.1 |1 | | |
|Seurat |? | | | |
|streamDAG |? | | | |
|package |version |error |warning |note |
|:------------|:-------|:------|:-------|:----|
|[akc](problems.md#akc)|0.9.9 |__+1__ | | |
|[archeofrag](problems.md#archeofrag)|0.8.2 |__+3__ | | |
|[arulesViz](problems.md#arulesviz)|1.5.3 |__+1__ | | |
|[bigergm](problems.md#bigergm)|1.1.0 |__+3__ | |1 |
|[bioregion](problems.md#bioregion)|1.1.1 |__+1__ | |1 |
|[bnviewer](problems.md#bnviewer)|0.1.6 |__+2__ | |3 |
|[causaloptim](problems.md#causaloptim)|0.9.8 |__+1__ | | |
|[cglasso](problems.md#cglasso)|2.0.7 |__+1__ | | |
|[corrViz](problems.md#corrviz)|0.1.0 |__+2__ | |1 |
|[cppRouting](problems.md#cpprouting)|3.1 |__+1__ | |4 |
|[cranly](problems.md#cranly)|0.6.0 |__+1__ | |1 |
|[criticalpath](problems.md#criticalpath)|0.2.1 |__+3__ | | |
|[debkeepr](problems.md#debkeepr)|0.1.1 |__+1__ | |1 |
|[DiagrammeR](problems.md#diagrammer)|1.0.11 |__+2__ | |2 |
|[dyndimred](problems.md#dyndimred)|1.0.4 |__+1__ | |2 |
|[dynwrap](problems.md#dynwrap)|1.2.4 |__+2__ | | |
|[egor](problems.md#egor)|1.24.2 |__+3__ | | |
|[epicontacts](problems.md#epicontacts)|1.1.4 |__+2__ | | |
|[erah](problems.md#erah)|2.0.1 |__+1__ | | |
|[fastRG](problems.md#fastrg)|0.3.2 |__+1__ | | |
|[fbnet](problems.md#fbnet)|1.0.3 |__+1__ | |1 |
|[GGally](problems.md#ggally)|2.2.1 |__+1__ | | |
|[ggraph](problems.md#ggraph)|2.2.1 |__+2__ | |1 |
|[grainscape](problems.md#grainscape)|0.4.4 |__+3__ | | |
|[graphlayouts](problems.md#graphlayouts)|1.1.1 |__+2__ | |2 |
|[intergraph](problems.md#intergraph)|2.0-4 |__+3__ | | |
|[interplex](problems.md#interplex)|0.1.2 |__+1__ | | |
|[LTFHPlus](problems.md#ltfhplus)|2.1.1 |__+1__ | | |
|[manynet](problems.md#manynet)|0.4.4 |__+2__ | |2 |
|[massiveGST](problems.md#massivegst)|1.2.3 |__+2__ | | |
|[MEGENA](problems.md#megena)|1.3.7 |__+1__ | | |
|[morph](problems.md#morph)|1.1.0 |__+1__ | | |
|[motifr](problems.md#motifr)|1.0.0 |__+1__ | | |
|[mstknnclust](problems.md#mstknnclust)|0.3.2 |__+2__ | | |
|[multinet](problems.md#multinet)|4.1.2 |__+1__ | |3 |
|[nda](problems.md#nda)|0.1.13 |__+1__ | | |
|[Neighboot](problems.md#neighboot)|1.0.1 |__+1__ | | |
|[pmd](problems.md#pmd)|0.2.1 |__+2__ | |2 |
|[polymapR](problems.md#polymapr)|1.1.5 |__+2__ | |1 |
|[ptools](problems.md#ptools)|2.0.0 |__+3__ | | |
|[RavenR](problems.md#ravenr)|2.2.2 |__+2__ | | |
|[rdracor](problems.md#rdracor)|1.0.3 |__+1__ | | |
|[ReDaMoR](problems.md#redamor)|0.7.4 |__+2__ | | |
|[rgexf](problems.md#rgexf)|0.16.2 |__+1__ | | |
|[RNewsflow](problems.md#rnewsflow)|1.2.8 |__+1__ | |1 |
|[rsetse](problems.md#rsetse)|0.5.0 |__+2__ | | |
|[rTwig](problems.md#rtwig)|1.0.2 |__+2__ | | |
|[skynet](problems.md#skynet)|1.4.3 |__+1__ | |1 |
|[SOMbrero](problems.md#sombrero)|1.4-2 |__+1__ | | |
|[TDApplied](problems.md#tdapplied)|3.0.3 |__+2__ | |1 |
|[textrank](problems.md#textrank)|0.3.1 |__+2__ | | |
|[tidygraph](problems.md#tidygraph)|1.3.1 |__+1__ | | |
|[timeordered](problems.md#timeordered)|1.0.0 |__+1__ | | |
|[wikkitidy](problems.md#wikkitidy)|0.1.12 |__+1__ | | |

Loading
Loading