问题
I'm finding that any call to get/set vertex attribute using the igraph library within callback causes a segfault in R. For example, the trivial callback from a segment of code:
dfsCallBack <- function(graph, data, extra) {
cat("in:", paste(collapse=", ", data), "\n")
distFromRoot <- data[2]
vertexID <- data[1]
set.vertex.attribute(graph, 0, name = 'color', value = 'blue')
FALSE
}
graph.dfs(g, 1, in.callback = dfsCallBack)
Produces this error:
graph.dfs(g, 1, in.callback = dfsCallBack)
in: 0, 0
*** caught segfault ***
address 0x0, cause 'memory not mapped'
Traceback:
1: .Call("R_igraph_dfs", graph, root, neimode, unreachable, as.logical(order), as.logical(order.out), as.logical(father), as.logical(dist), in.callback, out.callback, extra, rho, PACKAGE = "igraph")
2: graph.dfs(g, 1, in.callback = dfsCallBack)
Possible actions:
1: abort (with core dump, if enabled)
2: normal R exit
3: exit R without saving workspace
4: exit R saving workspace
Selection:
What's wrong here? Also, igraph should probably be more robust in dealing with these errors, crashing out of R is not ideal for a high-level language like R.
回答1:
A couple of points.
If you say
set.vertex.attribute(graph, ...)
,graph
is actually not changed, but a new copy is returned with the attribute updated. R objects are (almost always) immutable, you cannot change them, only create new objects based on them.set.vertex.attribute(graph, 0, name = 'color', value = 'blue')
is wrong, because vertex ids start at 1, so the 0 is invalid. This should be reported as an error, and was already fixed in our development tree.This is not an error, it is a bug. igraph errors do not crash R, they just give an error message. Because the igraph code and R run in the same thread, igraph bugs might crash R, and apart from avoiding bugs, there is not much we can do about this.
I would suggest to use the results of igraph.dfs
to set the attributes appropriately. You can of course use the callback to record information, or to terminate the DFS.
来源:https://stackoverflow.com/questions/17684034/r-igraph-any-call-to-get-set-vertex-attribute-within-a-depth-first-search-ca