Skip to content

Commit

Permalink
Merge pull request #145 from r-causal/strip_attr
Browse files Browse the repository at this point in the history
Manually strip graph attribute and test for nullness
  • Loading branch information
malcolmbarrett authored Mar 3, 2024
2 parents 802b690 + dd14ac9 commit 2dd12dd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
9 changes: 5 additions & 4 deletions R/tidy_dag.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>%
Expand Down
8 changes: 8 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 5 additions & 0 deletions tests/testthat/test-tidy_dag.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down

0 comments on commit 2dd12dd

Please sign in to comment.