Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R IGRAPHE graph.bfs和环境_R_Igraph - Fatal编程技术网

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