R IGRAPHE graph.bfs和环境
在图形上运行广度优先搜索算法时遇到问题,我当前关心的是环境参数,回调函数是在环境参数中计算的 这是回调函数R IGRAPHE graph.bfs和环境,r,igraph,R,Igraph,在图形上运行广度优先搜索算法时遇到问题,我当前关心的是环境参数,回调函数是在环境参数中计算的 这是回调函数 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(en
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)
}
}
f.in当文件说明:
rho:计算回调函数的环境
这意味着传递的环境是回调环境的父环境
因此,您可以使用get()
和parent.frame()
函数检索变量,如下例所示:
myCallBack <- function(graph, data, extra) {
print(ls(parent.frame()))
print(get('result_list', envir=parent.frame()))
stop('just to stop at the first call...')
}
# a simple tree with 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)
myCallBack <- function(graph, data, extra) {
print(ls(parent.frame()))
print(get('result_list', envir=parent.frame()))
stop('just to stop at the first call...')
}
# a simple tree with 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)
[1] "result_list"
$A
[1] 3