Skip to content
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
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)
})