From f1055eb236cbf8b6e4974471e71b48ad5bfcd94f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szabolcs=20Horva=CC=81t?= Date: Fri, 19 Jul 2024 09:53:27 +0200 Subject: [PATCH 1/2] fix: fix includig diagonal elements in dense adjacency matrices, fixes #1429 --- R/conversion.R | 16 +++++++++++++--- src/rinterface_extra.c | 2 +- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/R/conversion.R b/R/conversion.R index 6c9fdf9f07..63c0d20861 100644 --- a/R/conversion.R +++ b/R/conversion.R @@ -155,7 +155,7 @@ get.adjedgelist <- function(graph, mode = c("all", "out", "in", "total"), loops ################################################################### get.adjacency.dense <- function(graph, type = c("both", "upper", "lower"), - attr = NULL, weights = NULL, loops = FALSE, names = TRUE) { + attr = NULL, weights = NULL, loops = c("once", "twice", "ignore"), names = TRUE) { ensure_igraph(graph) type <- igraph.match.arg(type) @@ -165,13 +165,23 @@ get.adjacency.dense <- function(graph, type = c("both", "upper", "lower"), "both" = 2 ) + if (is.logical(loops)) { + loops <- ifelse(loops, "once", "ignore") + } + loops <- igraph.match.arg(loops) + loops <- switch(loops, + "ignore" = 0L, + "twice" = 1L, + "once" = 2L + ) + if (!is.null(weights)) weights <- as.numeric(weights) if (is.null(attr)) { on.exit(.Call(R_igraph_finalizer)) res <- .Call( R_igraph_get_adjacency, graph, as.numeric(type), weights, - as.logical(loops) + loops ) } else { attr <- as.character(attr) @@ -348,7 +358,7 @@ as_adjacency_matrix <- function(graph, type = c("both", "upper", "lower"), if (sparse) { get.adjacency.sparse(graph, type = type, attr = attr, names = names) } else { - get.adjacency.dense(graph, type = type, attr = attr, weights = NULL, names = names) + get.adjacency.dense(graph, type = type, attr = attr, weights = NULL, names = names, loops = "once") } } diff --git a/src/rinterface_extra.c b/src/rinterface_extra.c index 5c975b1541..4734d69da2 100644 --- a/src/rinterface_extra.c +++ b/src/rinterface_extra.c @@ -4796,7 +4796,7 @@ SEXP R_igraph_get_adjacency(SEXP graph, SEXP ptype, SEXP pweights, SEXP ploops) igraph_t g; igraph_matrix_t res; igraph_integer_t type=(igraph_integer_t) REAL(ptype)[0]; - igraph_bool_t loops=LOGICAL(ploops)[0]; + igraph_loops_t loops = (igraph_loops_t) INTEGER(ploops)[0]; igraph_vector_t weights; SEXP result; From 66d0e675347989b0556f210d3f1b71d98598a87e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Thu, 29 Aug 2024 09:55:35 +0200 Subject: [PATCH 2/2] add lifecycle message --- R/conversion.R | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/R/conversion.R b/R/conversion.R index 63c0d20861..5a09c61825 100644 --- a/R/conversion.R +++ b/R/conversion.R @@ -167,6 +167,10 @@ get.adjacency.dense <- function(graph, type = c("both", "upper", "lower"), if (is.logical(loops)) { loops <- ifelse(loops, "once", "ignore") + lifecycle::deprecate_soft( + "2.0.4", "get.adjacency.dense(loops = 'must be a character')", + details = sprintf("Converting to get.adjacency.dense (loops = '%s')", loops) + ) } loops <- igraph.match.arg(loops) loops <- switch(loops,