From dd14ac9f489857da4d0d7575b6257e29bd69a562 Mon Sep 17 00:00:00 2001 From: Malcolm Barrett Date: Sun, 3 Mar 2024 13:38:49 -0500 Subject: [PATCH] manually strip graph attribute and test for nullness --- R/tidy_dag.R | 9 +++++---- R/utils.R | 8 ++++++++ tests/testthat/test-tidy_dag.R | 5 +++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/R/tidy_dag.R b/R/tidy_dag.R index e455464..dd22f94 100644 --- a/R/tidy_dag.R +++ b/R/tidy_dag.R @@ -278,22 +278,23 @@ generate_layout <- function(.df, layout, vertices = NULL, coords = NULL, ...) { if (no_existing_coords) { - ggraph_layout <- suppressMessages(ggraph::create_layout( + ggraph_layout <- ggraph_create_layout( ig, layout = layout, ... - )) + ) + } else { nodes <- names(igraph::V(ig)) coords$x <- coords$x[nodes] coords$y <- coords$y[nodes] - ggraph_layout <- suppressMessages(ggraph::create_layout( + ggraph_layout <- ggraph_create_layout( ig, layout = "manual", x = coords$x, y = coords$y, ... - )) + ) } ggraph_layout %>% diff --git a/R/utils.R b/R/utils.R index e766eea..a24774a 100644 --- a/R/utils.R +++ b/R/utils.R @@ -201,3 +201,11 @@ check_arg_stylized <- function(stylized, use_stylized, what = "geom_dag") { use_stylized } + +ggraph_create_layout <- function(...) { + .df <- suppressMessages(ggraph::create_layout(...)) + # ggdag doesn't need the igraph object + attr(.df, "graph") <- NULL + + .df +} diff --git a/tests/testthat/test-tidy_dag.R b/tests/testthat/test-tidy_dag.R index 0704390..34aa371 100644 --- a/tests/testthat/test-tidy_dag.R +++ b/tests/testthat/test-tidy_dag.R @@ -120,6 +120,11 @@ test_that("Forbidden layouts error", { ) }) +test_that("igraph attribute does not hitchhike onto tidy dag", { + td <- tidy_dagitty(dagify(y ~ x + z, x ~ z)) + expect_null(attr(pull_dag_data(td), "graph")) +}) + expect_function_produces_name <- function(tidy_dag, column) { .df <- pull_dag_data(tidy_dag) expect_true(all(column %in% names(.df)))