我已经使用该d3r
包将igraph
对象转换为 JSON 格式,如下所示:
# create small world network
net <- sample_smallworld(size = 8, dim = 1, nei = 1, p = 0.33)
# convert to json with directional edges
data_json <- d3_igraph(as.directed(net))
这给了我以下输出:
> data_json
{"nodes":[{"id":"0"},{"id":"1"},{"id":"2"},{"id":"3"},{"id":"4"},{"id":"5"},{"id":"6"},{"id":"7"}],"links":[{"source":"0","target":"1"},{"source":"1","target":"6"},{"source":"2","target":"3"},{"source":"3","target":"4"},{"source":"0","target":"4"},{"source":"3","target":"5"},{"source":"6","target":"7"},{"source":"0","target":"7"},{"source":"1","target":"0"},{"source":"6","target":"1"},{"source":"3","target":"2"},{"source":"4","target":"3"},{"source":"4","target":"0"},{"source":"5","target":"3"},{"source":"7","target":"6"},{"source":"7","target":"0"}],"attributes":{"name":"Watts-Strogatz random graph","dim":1,"size":8,"nei":1,"p":0.33,"loops":false,"multiple":false}}
这几乎是我想要的,但我需要配置两件事:
- 我希望节点 ID 从 1 开始,而不是 0。
- 我想删除链接中节点 ID 周围的引号(引号很好,因为它们在节点元素中),以便每个链接都这样写
{"source": 1, "target": 2}
,而不是{"source": "1", "target": "2"}
我当然可以为这样的小型网络手动执行此操作,但这对于大型网络来说很乏味且不可扩展。有更好的方法R
吗?