I have the following code which will return a graph of a DAG:
library(dagitty)
library(ggplot2)
dag <- dagitty( "dag {
Y <- X <- Z1 <- V -> Z2 -> Y
Z1 <- W1 <-> W2 -> Z2
X <- W1 -> Y
X <- W2 -> Y
X [exposure]
Y [outcome]
}")
tidy_dag <- tidy_dagitty(dag, layout = "fr")
return_test <- function(tidy_df) {
tidy_df %>%
ggplot(aes(x = x, y = y, xend = xend, yend = yend)) +
geom_dag_node() +
geom_dag_text() +
geom_dag_edges() +
theme_dag()
}
return_test(tidy_dag)
I want to return both the dataframe and the graph though. How can I do this? Essentially I want to return the graph as well as tidy_dag
. Or maybe just return an object which I can use $ to get a specific element out of it.