Skip to content
Open
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
6 changes: 5 additions & 1 deletion R/attributes.R
Original file line number Diff line number Diff line change
Expand Up @@ -1256,7 +1256,11 @@ is_weighted <- function(graph) {
is_bipartite <- function(graph) {
ensure_igraph(graph)

"type" %in% vertex_attr_names(graph)
if (!"type" %in% vertex_attr_names(graph)) {
return(FALSE)
}
type_vals <- vertex_attr(graph, "type")
is.logical(type_vals) || !anyNA(as.logical(type_vals))
}

#############
Expand Down
20 changes: 15 additions & 5 deletions R/other.R
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,21 @@ handle_vertex_type_arg <- function(types, graph, required = T) {
}
if (!is.null(types)) {
if (!is.logical(types)) {
cli::cli_warn("vertex types converted to logical.")
}
types <- as.logical(types)
if (any(is.na(types))) {
cli::cli_abort("`NA' is not allowed in vertex types")
converted <- suppressWarnings(as.logical(types))
if (anyNA(converted)) {
cli::cli_abort(
"The {.arg type} vertex attribute is not logical and could not be \\
converted to logical. Please set it to a logical vector."
)
}
cli::cli_warn(
"The {.arg type} vertex attribute is not logical; converting to logical."
)
types <- converted
} else if (anyNA(types)) {
cli::cli_abort(
"The {.arg type} vertex attribute contains {.val NA} values, which are not allowed."
)
}
}
if (is.null(types) && required) {
Expand Down
23 changes: 23 additions & 0 deletions tests/testthat/test-attributes.R
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,29 @@ test_that("is_bipartite works", {
)
})

test_that("is_bipartite checks that type attribute is logical or convertible", {
g <- make_ring(4)

# No type attribute
expect_false(is_bipartite(g))

# Logical type
V(g)$type <- c(FALSE, TRUE, FALSE, TRUE)
expect_true(is_bipartite(g))

# Numeric 0/1 is convertible to logical
V(g)$type <- c(0, 1, 0, 1)
expect_true(is_bipartite(g))

# Character not convertible via as.logical (produces NAs)
V(g)$type <- c("a", "b", "a", "b")
expect_false(is_bipartite(g))

# NA-producing conversion
V(g)$type <- c(1L, 2L, 1L, NA_integer_)
expect_false(is_bipartite(g))
})

test_that("without_attr", {
withr::local_seed(42)
g_stripped <- sample_gnp(10, 2 / 10) %>%
Expand Down
Loading