-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support dagitty objects #183
Closed
Closed
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5c0d08c
support dagitty objects
grasshoppermouse 2cef20c
Added node attributes for which there are getter functions. Added arg…
grasshoppermouse b245d80
Complete rewrite. Now handles multiple user-defined node and edge att…
grasshoppermouse 8e18e25
fixed error in empty edges code
grasshoppermouse f6ba04e
Removed use of internal functions
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ Suggests: | |
ape, | ||
covr, | ||
data.tree, | ||
dagitty, | ||
graph, | ||
influenceR, | ||
methods, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,44 @@ | ||||||
#' @describeIn tbl_graph Method to deal with dagitty objects from the dagitty package | ||||||
#' @export | ||||||
#' @importFrom rlang is_empty check_installed | ||||||
as_tbl_graph.dagitty <- function(x, directed = TRUE, node_attr = NULL, edge_attr = NULL, ...) { | ||||||
check_installed('dagitty', 'in order to coerce a dagitty object to tbl_graph') | ||||||
if (!is_empty(intersect(node_attr, c('adjusted', 'latent', 'exposure', 'outcome')))) stop('node_attr cannot be adjusted, exposure, outcome, or latent, as these are automatically added if present') | ||||||
if (is_empty(names(x))) return(create_empty(0)) | ||||||
|
||||||
nodes <- tibble::tibble( | ||||||
name = names(x) | ||||||
) | ||||||
adjusted <- dagitty::adjustedNodes(x) | ||||||
if (!is_empty(adjusted)) nodes$adjusted <- nodes$name %in% adjusted | ||||||
exposure <- dagitty::exposures(x) | ||||||
if (!is_empty(exposure)) nodes$exposure <- nodes$name %in% exposure | ||||||
outcome <- dagitty::outcomes(x) | ||||||
if (!is_empty(outcome)) nodes$outcome <- nodes$name %in% outcome | ||||||
latent <- dagitty::latents(x) | ||||||
if (!is_empty(latent)) nodes$latent <- nodes$name %in% latent | ||||||
coords <- coordinates(x) | ||||||
if (!all(is.na(coords$x))) { | ||||||
nodes$x <- coords$x | ||||||
nodes$y <- coords$y | ||||||
} | ||||||
|
||||||
for (a in node_attr){ | ||||||
if (vctrs::vec_as_names(a, repair = 'unique') != a) stop('each node_attr must be a string of length > 0') | ||||||
nodes[a] <- dagitty:::.vertexAttributes(x, a)$a | ||||||
} | ||||||
|
||||||
edges <- dagitty::edges(x) | ||||||
if (is_empty(edges)){ | ||||||
edges <- tibble::tibble(from = int(), to = int()) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} else { | ||||||
edges <- edges[c('v', 'w', 'e')] | ||||||
names(edges) <- c('from', 'to', 'type') | ||||||
for (a in edge_attr){ | ||||||
if (vctrs::vec_as_names(a, repair = 'unique') != a) stop('each edge_attr must be a string of length > 0') | ||||||
edges[a] <- dagitty:::.edgeAttributes(x, a)$a | ||||||
} | ||||||
} | ||||||
|
||||||
tbl_graph(nodes = nodes, edges = edges, directed = directed) | ||||||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there is no way to extract these without using a non-exported function we should simply ignore any additional attributes all-together. Same with edge attributes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Except that the "beta" edge attributes are important. Perhaps @jtextor, maintainer of the dagitty package, can suggest a solution, e.g., modifying the edges function to also extract the beta values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, that would be necessary on daggity's side. Using internal functions from other packages is not allowed, nor wise