Skip to content

Commit 535970b

Browse files
committed
refactor: throw early when plotting empty graphs
1 parent 03bc74d commit 535970b

3 files changed

Lines changed: 18 additions & 3 deletions

File tree

R/plot-core.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ S7::method(plot, caugi) <- function(
150150
) {
151151
is_caugi(x, throw_error = TRUE)
152152

153+
if (is_empty_caugi(x)) {
154+
stop("Cannot plot an empty graph (0 nodes).", call. = FALSE)
155+
}
156+
153157
stopifnot(
154158
is.null(asp) ||
155159
is.na(asp) ||

R/plot-grobs.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ make_tiers <- function(
5555
}
5656

5757
# Normalize to 0-indexed
58-
min_tier <- min(assignments)
59-
if (min_tier > 0) {
60-
assignments <- assignments - min_tier
58+
if (length(assignments) > 0) {
59+
min_tier <- min(assignments)
60+
if (min_tier > 0) {
61+
assignments <- assignments - min_tier
62+
}
6163
}
6264

6365
# Extract components

tests/testthat/test-plot.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,15 @@ test_that("plot-grobs internal branches are covered", {
888888
expect_equal(length(c2$children), 3L)
889889
})
890890

891+
test_that("plotting empty graph gives clear error", {
892+
cg_empty <- caugi(class = "DAG")
893+
894+
expect_error(
895+
plot(cg_empty),
896+
"Cannot plot an empty graph \\(0 nodes\\)\\."
897+
)
898+
})
899+
891900
test_that("plot layout validation branch is covered", {
892901
cg <- caugi(
893902
from = LETTERS[1:7],

0 commit comments

Comments
 (0)