From 60ece5793ba735dde7afa50c88b78c6a98b73fa7 Mon Sep 17 00:00:00 2001 From: schochastics Date: Thu, 3 Jul 2025 15:24:29 +0200 Subject: [PATCH 1/4] reffactored edge attribute to match vertex attribute --- R/attributes.R | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/R/attributes.R b/R/attributes.R index 7bac81c39fa..1514ccfc7b9 100644 --- a/R/attributes.R +++ b/R/attributes.R @@ -618,7 +618,7 @@ vertex.attributes <- function(graph, index = V(graph)) { ) if (!missing(index)) { - if (!index_is_natural_sequence(index, graph)) { + if (!index_is_natural_sequence(index, graph, type = "vertices")) { for (i in seq_along(res)) { res[[i]] <- res[[i]][index] } @@ -658,7 +658,10 @@ set_value_at <- function(value, idx, length_out) { } } - if (!missing(index) && !index_is_natural_sequence(index, graph)) { + if ( + !missing(index) && + !index_is_natural_sequence(index, graph, type = "vertices") + ) { value <- map( value, set_value_at, @@ -869,7 +872,7 @@ edge.attributes <- function(graph, index = E(graph)) { if ( !missing(index) && - (length(index) != ecount(graph) || any(index != E(graph))) + !index_is_natural_sequence(index, graph, type = "edges") ) { for (i in seq_along(res)) { res[[i]] <- res[[i]][index] @@ -900,16 +903,14 @@ edge.attributes <- function(graph, index = E(graph)) { if ( !missing(index) && - (length(index) != ecount(graph) || any(index != E(graph))) + !index_is_natural_sequence(index, graph, type = "edges") ) { - es <- E(graph) - for (i in seq_along(value)) { - tmp <- value[[i]] - length(tmp) <- 0 - length(tmp) <- length(es) - tmp[index] <- value[[i]] - value[[i]] <- tmp - } + value <- map( + value, + set_value_at, + idx = index, + length_out = length(E(graph)) + ) } .Call( @@ -1442,6 +1443,12 @@ assert_named_list <- function(value) { } } -index_is_natural_sequence <- function(index, graph) { - length(index) == vcount(graph) && all(index == seq_len(vcount(graph))) +index_is_natural_sequence <- function(index, graph, type = "vertices") { + if (type == "vertices") { + length(index) == vcount(graph) && all(index == seq_len(vcount(graph))) + } else if (type == "edges") { + length(index) == ecount(graph) && all(index == seq_len(ecount(graph))) + } else { + cli::cli_abort("Unknown type {.arg {type}} for index.") + } } From f40ccb2310bcac0fa2378ed4a6dae3c70ee2fcd2 Mon Sep 17 00:00:00 2001 From: schochastics Date: Mon, 7 Jul 2025 20:18:54 +0200 Subject: [PATCH 2/4] more elegant natural_sequence --- R/attributes.R | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/R/attributes.R b/R/attributes.R index 1514ccfc7b9..1ac4462b1fe 100644 --- a/R/attributes.R +++ b/R/attributes.R @@ -1444,11 +1444,10 @@ assert_named_list <- function(value) { } index_is_natural_sequence <- function(index, graph, type = "vertices") { - if (type == "vertices") { - length(index) == vcount(graph) && all(index == seq_len(vcount(graph))) - } else if (type == "edges") { - length(index) == ecount(graph) && all(index == seq_len(ecount(graph))) - } else { - cli::cli_abort("Unknown type {.arg {type}} for index.") - } + count <- switch( + type, + vertices = vcount(graph), + edges = ecount(graph) + ) + length(index) == count && all(index == seq_len(count)) } From 01b26a713fb3c2c9bc675bfadfc3d8366aefbe4d Mon Sep 17 00:00:00 2001 From: schochastics Date: Mon, 7 Jul 2025 20:31:58 +0200 Subject: [PATCH 3/4] added test --- tests/testthat/test-attributes.R | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/testthat/test-attributes.R b/tests/testthat/test-attributes.R index 9736d30b062..9c7fdbfed34 100644 --- a/tests/testthat/test-attributes.R +++ b/tests/testthat/test-attributes.R @@ -454,9 +454,13 @@ test_that("empty returns work", { test_that("assign data.frame attributes works", { # https://github.com/igraph/rigraph/issues/1669 - g <- make_tree(10, 3) - edge.attributes(g) <- head(mtcars, ecount(g)) - expect_no_error(E(g)[c(1, 2)]) + g1 <- make_tree(10, 3) + edge.attributes(g1) <- head(mtcars, ecount(g1)) + expect_no_error(E(g1)[c(1, 2)]) + + g2 <- make_tree(10, 3) + edge.attributes(g2, E(g2)[1:5]) <- head(mtcars, 5) + expect_equal(E(g2)$wt, c(mtcars$wt[1:5], rep(NA, ecount(g2) - 5))) }) test_that("good error message when not using character", { From ed183165d8750d94dda0281992849023c946631c Mon Sep 17 00:00:00 2001 From: schochastics Date: Thu, 10 Jul 2025 13:01:40 +0200 Subject: [PATCH 4/4] separated index_is_natural_sequence into two functions --- R/attributes.R | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/R/attributes.R b/R/attributes.R index 1ac4462b1fe..5a91efe864d 100644 --- a/R/attributes.R +++ b/R/attributes.R @@ -618,7 +618,7 @@ vertex.attributes <- function(graph, index = V(graph)) { ) if (!missing(index)) { - if (!index_is_natural_sequence(index, graph, type = "vertices")) { + if (!index_is_natural_vertex_sequence(index, graph)) { for (i in seq_along(res)) { res[[i]] <- res[[i]][index] } @@ -660,7 +660,7 @@ set_value_at <- function(value, idx, length_out) { if ( !missing(index) && - !index_is_natural_sequence(index, graph, type = "vertices") + !index_is_natural_vertex_sequence(index, graph) ) { value <- map( value, @@ -872,7 +872,7 @@ edge.attributes <- function(graph, index = E(graph)) { if ( !missing(index) && - !index_is_natural_sequence(index, graph, type = "edges") + !index_is_natural_edge_sequence(index, graph) ) { for (i in seq_along(res)) { res[[i]] <- res[[i]][index] @@ -903,7 +903,7 @@ edge.attributes <- function(graph, index = E(graph)) { if ( !missing(index) && - !index_is_natural_sequence(index, graph, type = "edges") + !index_is_natural_edge_sequence(index, graph) ) { value <- map( value, @@ -1443,11 +1443,12 @@ assert_named_list <- function(value) { } } -index_is_natural_sequence <- function(index, graph, type = "vertices") { - count <- switch( - type, - vertices = vcount(graph), - edges = ecount(graph) - ) +index_is_natural_vertex_sequence <- function(index, graph) { + count <- vcount(graph) + length(index) == count && all(index == seq_len(count)) +} + +index_is_natural_edge_sequence <- function(index, graph) { + count <- ecount(graph) length(index) == count && all(index == seq_len(count)) }