【发布时间】:2017-01-02 04:36:55
【问题描述】:
我尝试使用igraph::topological.sort() 来获得图的拓扑排序并检查图是否有循环。该函数仅在图形包含循环时发出警告并返回部分结果,但是当图形循环时我必须停止处理,因此我在 tryCatch() 函数中调用 igraph::topological.sort()。
然后 R 解释器被核心转储了。
重现的最少代码:
library(igraph)
# This has a cycle
adjacency_with_cycle <- matrix(c(0,1,0,1,0,0,0,1,0), 3, 3)
g_with_cycle <- graph_from_adjacency_matrix(adjacency_with_cycle)
# This doesn't
adjacency_without_cycle <- matrix(c(0,1,0,0,0,0,0,1,0), 3, 3)
g_without_cycle <- graph_from_adjacency_matrix(adjacency_without_cycle)
# Codes below moves
## Only warns on the graph with cycle.
topological.sort(g_with_cycle)
## No problem on the acyclic graph.
topological.sort(g_without_cycle)
## Call in tryCatch. but no warning
tryCatch({
topological.sort(g_without_cycle)
},
warning = function (w) stop())
# Just a tryCatch
tryCatch({warning("warn")}, warning = function (w) stop("stop"))
# Core dumped when catching warning
tryCatch({
topological.sort(g_with_cycle)
},
warning = function (w) stop()) My environments are:
我的环境是(两个都出现问题)
- Windwos 10,MRO R 3.3.1,igraph 1.0.1
- Manjaro Linux,R 3.3.2,igraph 1.0.1
鉴于以上,我想知道
- 这是
igraph包的问题,还是其他问题? - 为什么会出现这种情况?
tryCatch()在捕获警告方面做了什么?
【问题讨论】:
标签: r try-catch warnings igraph