From 193825004dbbefded15cd979a453f124d682907a Mon Sep 17 00:00:00 2001 From: schochastics Date: Tue, 24 Jun 2025 10:53:57 +0200 Subject: [PATCH 1/3] added warning --- R/plot.R | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/R/plot.R b/R/plot.R index 43c75141150..f3f881fb3d1 100644 --- a/R/plot.R +++ b/R/plot.R @@ -135,6 +135,11 @@ plot.igraph <- function( } layout <- i.postprocess.layout(params("plot", "layout")) + if (nrow(layout) != vc) { + cli::cli_warn( + "The layout has {nrow(layout)} rows, but the graph has {vc} vertices. Unintended results may occur." + ) + } margin <- params("plot", "margin") margin <- rep(margin, length.out = 4) rescale <- params("plot", "rescale") From db4f62ae3735f068727d4f32b2465ab6d953b27b Mon Sep 17 00:00:00 2001 From: schochastics Date: Tue, 24 Jun 2025 20:13:40 +0200 Subject: [PATCH 2/3] augmented error message --- R/plot.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/R/plot.R b/R/plot.R index f3f881fb3d1..fbbfb274512 100644 --- a/R/plot.R +++ b/R/plot.R @@ -136,9 +136,9 @@ plot.igraph <- function( layout <- i.postprocess.layout(params("plot", "layout")) if (nrow(layout) != vc) { - cli::cli_warn( - "The layout has {nrow(layout)} rows, but the graph has {vc} vertices. Unintended results may occur." - ) + cli::cli_warn(c( + "The layout has {nrow(layout)} rows, but the graph has {vc} vertices. Unintended results may occur.", + "i" = "It is recommended to store the layout as x and y vertex attributes and not as a matrix graph attribute.")) } margin <- params("plot", "margin") margin <- rep(margin, length.out = 4) From 24fecf386ee910f19db300b7f3e916dd9b4d25a2 Mon Sep 17 00:00:00 2001 From: schochastics Date: Mon, 7 Jul 2025 14:30:37 +0200 Subject: [PATCH 3/3] made error added test --- R/plot.R | 4 ++-- tests/testthat/_snaps/plot.md | 9 +++++++++ tests/testthat/test-plot.R | 8 ++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 tests/testthat/_snaps/plot.md diff --git a/R/plot.R b/R/plot.R index fbbfb274512..0bcd8a9a59d 100644 --- a/R/plot.R +++ b/R/plot.R @@ -136,8 +136,8 @@ plot.igraph <- function( layout <- i.postprocess.layout(params("plot", "layout")) if (nrow(layout) != vc) { - cli::cli_warn(c( - "The layout has {nrow(layout)} rows, but the graph has {vc} vertices. Unintended results may occur.", + cli::cli_abort(c( + "The layout has {nrow(layout)} rows, but the graph has {vc} vertices.", "i" = "It is recommended to store the layout as x and y vertex attributes and not as a matrix graph attribute.")) } margin <- params("plot", "margin") diff --git a/tests/testthat/_snaps/plot.md b/tests/testthat/_snaps/plot.md new file mode 100644 index 00000000000..4d902f54dae --- /dev/null +++ b/tests/testthat/_snaps/plot.md @@ -0,0 +1,9 @@ +# layout as graph attribute error works + + Code + plot(g) + Condition + Error in `plot()`: + ! The layout has 5 rows, but the graph has 10 vertices. + i It is recommended to store the layout as x and y vertex attributes and not as a matrix graph attribute. + diff --git a/tests/testthat/test-plot.R b/tests/testthat/test-plot.R index d444050058f..12f51bd2543 100644 --- a/tests/testthat/test-plot.R +++ b/tests/testthat/test-plot.R @@ -131,3 +131,11 @@ test_that("Edges stop at outside of rectangle node", { vdiffr::expect_doppelganger("rectangle-edges", rectangle_edges) }) + +test_that("layout as graph attribute error works", { + g <- make_full_graph(10) + g$layout <- layout_in_circle(g)[1:5, ] + expect_snapshot(error = TRUE, { + plot(g) + }) +})