diff --git a/DESCRIPTION b/DESCRIPTION index 6d16731..9e7598b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -33,6 +33,7 @@ Suggests: ape, covr, data.tree, + dagitty, graph, influenceR, methods, diff --git a/NAMESPACE b/NAMESPACE index cda68ef..ca0ab8b 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) @@ -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) diff --git a/R/dagitty.R b/R/dagitty.R new file mode 100644 index 0000000..b752ed4 --- /dev/null +++ b/R/dagitty.R @@ -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) +} diff --git a/man/tbl_graph.Rd b/man/tbl_graph.Rd index 2f2eb1e..b404de1 100644 --- a/man/tbl_graph.Rd +++ b/man/tbl_graph.Rd @@ -1,8 +1,9 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/data_frame.R, R/data_tree.R, R/dendrogram.R, -% R/graph.R, R/hclust.R, R/igraph.R, R/list.R, R/matrix.R, R/network.R, -% R/phylo.R, R/tbl_graph.R -\name{as_tbl_graph.data.frame} +% Please edit documentation in R/dagitty.R, R/data_frame.R, R/data_tree.R, +% R/dendrogram.R, R/graph.R, R/hclust.R, R/igraph.R, R/list.R, R/matrix.R, +% R/network.R, R/phylo.R, R/tbl_graph.R +\name{as_tbl_graph.dagitty} +\alias{as_tbl_graph.dagitty} \alias{as_tbl_graph.data.frame} \alias{as_tbl_graph.Node} \alias{as_tbl_graph.dendrogram} @@ -22,6 +23,8 @@ \alias{is.tbl_graph} \title{A data structure for tidy graph manipulation} \usage{ +\method{as_tbl_graph}{dagitty}(x, directed = TRUE, ...) + \method{as_tbl_graph}{data.frame}(x, directed = TRUE, ...) \method{as_tbl_graph}{Node}(x, directed = TRUE, mode = "out", ...) @@ -106,6 +109,8 @@ supported. } \section{Functions}{ \itemize{ +\item \code{as_tbl_graph(dagitty)}: Method to deal with dagitty objects from the dagitty package + \item \code{as_tbl_graph(data.frame)}: Method for edge table and set membership table \item \code{as_tbl_graph(Node)}: Method to deal with Node objects from the data.tree package