I have used the d3r
package to convert an igraph
object to JSON format like so:
# 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))
This gives me the following output:
> 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}}
This is nearly what I want, but I need to configure two things:
- I want node IDs to start at 1, not 0.
- I want to remove the quotes around the node IDs in the links (the quotes are fine as they are in the nodes element), so that each link is written like this
{"source": 1, "target": 2}
, instead of{"source": "1", "target": "2"}
I could of course just do this manually for a small network like this, but that's tedious and not scalable for large networks. Is there better way I can this is R
?