R iGraph graph.bfs and environment -
having trouble running breadth-first search algorithm on graph, current concern environment parameter, callback function evaluated.
this callback function
f.in <- function(graph, data, extra) { time <- get.vertex.attribute(graph, "time", index=data["vid"]) root_time <- get.vertex.attribute(graph, "time", index=extra) print(ls(environment())) if (time != 0){ time_difference <- time - root_time result_list <- c(list(), time_difference) } }
this context functions called
graphs <- decompose.graph(network_graph, max.comps = na, min.vertices = 0) lapply(graphs, function(g){ v0 <- which(degree(g, mode="out") == 0) t0 <- get.vertex.attribute(g, "time", index=v0) if (t0 != 0) { bfs_environment <- new.env() assign("result_list", list(), envir=bfs_environment) graph.bfs(g, v0, neimode="in", callback=f.in, extra=v0, rho=bfs_environment) } })
now print of environment shows me following variables "data" "extra" "graph" "root_time" "time"
the question when have passed environment callback function evaluated, why "result_list" not available? there wrong passing environment? btw using r 2.15.3 , igraph 0.7.0
when documentation states:
rho: environment in callback function evaluated
it means passed environment parent
of callback environment.
so can retrieve variable using get()
, parent.frame()
functions, shown in following example:
mycallback <- function(graph, data, extra) { print(ls(parent.frame())) print(get('result_list', envir=parent.frame())) stop('just stop @ first call...') } # simple tree 3 nodes g <- graph.tree(3, children = 2, mode='out') bfs_environment <- new.env() assign("result_list", list(a=3), envir=bfs_environment) graph.bfs(g, 1, callback=mycallback, extra=null, rho=bfs_environment)
output:
[1] "result_list" $a [1] 3
Comments
Post a Comment