Skip to content
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
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Suggests:
ape,
covr,
data.tree,
dagitty,
graph,
influenceR,
methods,
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ S3method(as.data.frame,tbl_graph)
S3method(as.igraph,tbl_graph)
S3method(as.list,tbl_graph)
S3method(as_tbl_graph,Node)
S3method(as_tbl_graph,dagitty)
S3method(as_tbl_graph,data.frame)
S3method(as_tbl_graph,default)
S3method(as_tbl_graph,dendrogram)
Expand Down Expand Up @@ -593,11 +594,13 @@ importFrom(rlang,.data)
importFrom(rlang,UQS)
importFrom(rlang,as_quosure)
importFrom(rlang,caller_env)
importFrom(rlang,check_installed)
importFrom(rlang,enexpr)
importFrom(rlang,enquo)
importFrom(rlang,eval_bare)
importFrom(rlang,eval_tidy)
importFrom(rlang,is_bare_list)
importFrom(rlang,is_empty)
importFrom(rlang,list2)
importFrom(rlang,quo)
importFrom(rlang,quo_text)
Expand Down
34 changes: 34 additions & 0 deletions R/dagitty.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#' @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, ...) {
check_installed('dagitty', 'in order to coerce a dagitty object to tbl_graph')
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 <- dagitty::coordinates(x)
if (!all(is.na(coords$x))) {
nodes$x <- coords$x
nodes$y <- coords$y
}

e <- dagitty::edges(x)
if (is_empty(e)){
e <- tibble::tibble(from = integer(), to = integer())
} else {
e <- e[c('v', 'w', 'e')]
names(e) <- c('from', 'to', 'type')
}

tbl_graph(nodes = nodes, edges = e, directed = directed)
}
13 changes: 9 additions & 4 deletions man/tbl_graph.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.