Reputation: 89
I have a function that lists all the accessible environments in the current session.
Edited output:
[[1]]
<environment: R_GlobalEnv>
[[2]]
<environment: package:igraph>
attr(,"name")
[1] "package:igraph"
attr(,"path")
[1] "/Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/library/igraph"
[[3]]
<environment: package:stats>
attr(,"name")
[1] "package:stats"
attr(,"path")
[1] "/Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/library/stats"
[[4]]
<environment: R_EmptyEnv>
[[5]]
<environment: namespace:igraph>
[[6]]
<environment: namespace:stats>
Could you advise approach to convert this list to a graph? I want to plot it with igraph later. Elements 1, 2, 3 probably should be nodes, but then what is the best way to denote edges, connecting parent and child environments?
Upvotes: 1
Views: 241
Reputation: 173898
The obvious way to do this is to create an edgelist as a two-column character matrix with each environment's name in the left column, and its parent's name in the right column:
library(igraph)
my_envs <- env_list(globalenv())
edgelist <- t(sapply(my_envs[-length(my_envs)], function(x) {
c(environmentName(x), environmentName(parent.env(x)))
}))
g <- graph_from_edgelist(edgelist)
plot(g)
Upvotes: 2