Skip to content
Draft
Show file tree
Hide file tree
Changes from 3 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
13 changes: 13 additions & 0 deletions R/operators.R
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,13 @@ path <- function(...) {
e2[named]
)

# Warn if adding named vertices to an unnamed graph
if (!is.null(nn) && !is_named(e1)) {
cli::cli_warn(
"Adding named vertices to an unnamed graph. Existing vertices will have {.code NA} names."
)
}

# When adding vertices via +, all unnamed arguments are interpreted as vertex names of the new vertices.
res <- add_vertices(e1, nv = vctrs::vec_size_common(!!!e2), attr = e2)
} else if ("igraph.path" %in% class(e2)) {
Expand Down Expand Up @@ -1188,6 +1195,12 @@ path <- function(...) {
res <- add_vertices(e1, e2)
} else if (is.character(e2)) {
## Adding named vertices
# Warn if adding named vertices to an unnamed graph
if (!is_named(e1)) {
cli::cli_warn(
"Adding named vertices to an unnamed graph. Existing vertices will have {.code NA} names."
)
}
res <- add_vertices(e1, length(e2), name = e2)
} else {
cli::cli_abort("Cannot add {.obj_type_friendly {type}} to igraph graph.")
Expand Down
16 changes: 10 additions & 6 deletions tests/testthat/test-make.R
Original file line number Diff line number Diff line change
Expand Up @@ -231,17 +231,21 @@ test_that("make_graph works for numeric edges and isolates", {

test_that("make_graph handles names", {
graph_make_names <- make_graph(letters[1:10])
graph_elist_names <- make_empty_graph() +
vertices(letters[1:10]) +
edges(letters[1:10])
graph_elist_names <- suppressWarnings(
make_empty_graph() +
vertices(letters[1:10]) +
edges(letters[1:10])
)
expect_identical_graphs(graph_make_names, graph_elist_names)
})

test_that("make_graph handles names and isolates", {
graph_make_iso <- make_graph(letters[1:10], isolates = letters[11:20])
graph_elist_iso <- make_empty_graph() +
vertices(letters[1:20]) +
edges(letters[1:10])
graph_elist_iso <- suppressWarnings(
make_empty_graph() +
vertices(letters[1:20]) +
edges(letters[1:10])
)
expect_identical_graphs(graph_make_iso, graph_elist_iso)
})

Expand Down
54 changes: 48 additions & 6 deletions tests/testthat/test-operators.R
Original file line number Diff line number Diff line change
Expand Up @@ -214,24 +214,28 @@ test_that("t() is aliased to edge reversal for graphs", {
})

test_that("vertices() works", {
g_all_unnamed <- make_empty_graph(1) + vertices("a", "b")
g_all_unnamed <- suppressWarnings(make_empty_graph(1) + vertices("a", "b"))
expect_s3_class(V(g_all_unnamed), "igraph.vs")
expect_identical(V(g_all_unnamed)$name, c(NA, "a", "b"))

g_mix_named_unnamed <- make_empty_graph(1) + vertices("a", "b", foo = 5)
g_mix_named_unnamed <- suppressWarnings(
make_empty_graph(1) + vertices("a", "b", foo = 5)
)
expect_s3_class(V(g_mix_named_unnamed), "igraph.vs")
expect_true(is.na(V(g_mix_named_unnamed)$name[1]))
expect_identical(V(g_mix_named_unnamed)$name[-1], c("a", "b"))
expect_equal(V(g_mix_named_unnamed)$foo, c(NA, 5, 5))

g_mix_bigger_attribute <- make_empty_graph(1) +
vertices("a", "b", "c", foo = 5:7, bar = 8)
g_mix_bigger_attribute <- suppressWarnings(
make_empty_graph(1) +
vertices("a", "b", "c", foo = 5:7, bar = 8)
)
expect_s3_class(V(g_mix_bigger_attribute), "igraph.vs")
expect_identical(V(g_mix_bigger_attribute)$name, c(NA, "a", "b", "c"))
expect_equal(V(g_mix_bigger_attribute)$foo, c(NA, 5, 6, 7))
expect_equal(V(g_mix_bigger_attribute)$bar, c(NA, 8, 8, 8))

g_one_unnamed <- make_empty_graph(1) + vertices(letters)
g_one_unnamed <- suppressWarnings(make_empty_graph(1) + vertices(letters))
expect_s3_class(V(g_one_unnamed), "igraph.vs")
expect_identical(V(g_one_unnamed)$name, c(NA, letters))

Expand All @@ -249,7 +253,9 @@ test_that("vertices() works", {
expect_s3_class(V(g_none), "igraph.vs")
expect_null(V(g_none)$name)

expect_snapshot_error(make_empty_graph(1) + vertices("a", "b", foo = 5:7))
expect_snapshot_error(suppressWarnings(
make_empty_graph(1) + vertices("a", "b", foo = 5:7)
))
})

test_that("vertices() errors on duplicate attribute names", {
Expand All @@ -274,6 +280,42 @@ test_that("vertices() errors on duplicate attribute names", {
)
})

test_that("adding named vertices to unnamed graphs warns", {
# Test with vertex() function
expect_warning(
make_ring(10) + vertex(1),
"Adding named vertices to an unnamed graph"
)

expect_warning(
make_ring(10) + vertex("a"),
"Adding named vertices to an unnamed graph"
)

expect_warning(
make_ring(10) + vertices("a", "b"),
"Adding named vertices to an unnamed graph"
)

# Test with character vector
expect_warning(
make_ring(10) + c("a", "b"),
"Adding named vertices to an unnamed graph"
)

# No warning when adding to named graph
g <- make_ring(10)
V(g)$name <- letters[1:10]
expect_no_warning(g + vertex("k"))
expect_no_warning(g + c("x", "y"))

# No warning when adding unnamed vertices
expect_no_warning(make_ring(10) + vertex(foo = 5))
expect_no_warning(make_ring(10) + vertices(foo = 1:3))
expect_no_warning(make_ring(10) + 5)
})


test_that("infix operators work", {
g <- make_ring(10)
V(g)$name <- letters[1:10]
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-print.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ test_that("print.igraph() works", {
})
expect_output(print(g6), " ")

kite <- make_empty_graph(directed = FALSE) + LETTERS[1:10]
kite <- suppressWarnings(make_empty_graph(directed = FALSE) + LETTERS[1:10])
kite <- kite +
edges(
"A", "B",
Expand Down
16 changes: 10 additions & 6 deletions tests/testthat/test-topology.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ test_that("automorphism_group works with colored graphs", {
})

test_that("isomorphisms() works", {
motif <- make_empty_graph(directed = FALSE) +
vertices("D1", "D2", type = c("type1", "type1")) +
edges("D1", "D2", type = c("type2"))
motif <- suppressWarnings(
make_empty_graph(directed = FALSE) +
vertices("D1", "D2", type = c("type1", "type1")) +
edges("D1", "D2", type = c("type2"))
)
motif_iso <- isomorphisms(
motif,
motif,
Expand All @@ -61,9 +63,11 @@ test_that("isomorphisms() works", {
})

test_that("subgraph_isomorphisms works", {
motif <- make_empty_graph(directed = FALSE) +
vertices("D1", "D2", type = c("type1", "type1")) +
edges("D1", "D2", type = c("type2"))
motif <- suppressWarnings(
make_empty_graph(directed = FALSE) +
vertices("D1", "D2", type = c("type1", "type1")) +
edges("D1", "D2", type = c("type2"))
)
out <- subgraph_isomorphisms(
target = motif,
pattern = motif,
Expand Down
Loading