Skip to content

Commit

Permalink
fix: Recycling of logical vectors when indexing into edge/vertex sele…
Browse files Browse the repository at this point in the history
…ctors now throws an error (#1731)
  • Loading branch information
schochastics authored Mar 6, 2025
1 parent dd6f24f commit 7850bef
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
8 changes: 8 additions & 0 deletions R/iterators.R
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,10 @@ simple_vs_index <- function(x, i, na_ok = FALSE) {
if (is.null(ii)) {
return(NULL)
}
if (is.logical(ii) && (length(ii) != length(x) && length(ii) != 1)) {
cli::cli_abort("Error: Logical index length does not match the number of vertices. Recycling is not allowed.")
}

ii <- simple_vs_index(x, ii, na_ok)
attr(ii, "env") <- attr(x, "env")
attr(ii, "graph") <- attr(x, "graph")
Expand Down Expand Up @@ -991,6 +995,10 @@ simple_es_index <- function(x, i, na_ok = FALSE) {
if (is.null(ii)) {
return(NULL)
}
if (is.logical(ii) && (length(ii) != length(x) && length(ii) != 1)) {
cli::cli_abort("Error: Logical index length does not match the number of edges. Recycling is not allowed.")
}

ii <- simple_es_index(x, ii)
attr(ii, "env") <- attr(x, "env")
attr(ii, "graph") <- attr(x, "graph")
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/_snaps/iterators.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,19 @@
+ 10/? edges (deleted) (vertex names):
[1] a|b b|c c|d d|e e|f f|g g|h h|i i|j a|j

# logical indices are not recycled

Code
V(g)[c(TRUE, FALSE)]
Condition
Error in `FUN()`:
! Error: Logical index length does not match the number of vertices. Recycling is not allowed.

---

Code
E(g)[c(TRUE, FALSE)]
Condition
Error in `FUN()`:
! Error: Logical index length does not match the number of edges. Recycling is not allowed.

7 changes: 7 additions & 0 deletions tests/testthat/test-iterators.R
Original file line number Diff line number Diff line change
Expand Up @@ -440,3 +440,10 @@ test_that("edge indexes are stored as raw numbers", {
expect_identical(E(g)$id, as.numeric(1:3))
expect_error(induced_subgraph(g, 1:2), NA)
})

test_that("logical indices are not recycled", {
# https://github.com/igraph/rigraph/issues/848
g <- make_ring(5)
expect_snapshot(V(g)[c(TRUE, FALSE)], error = TRUE)
expect_snapshot(E(g)[c(TRUE, FALSE)], error = TRUE)
})

0 comments on commit 7850bef

Please sign in to comment.