christophergandrud / networkD3

D3 JavaScript Network Graphs from R
http://christophergandrud.github.io/networkD3
649 stars 269 forks source link

re-index nodes from 0 if necessary #190

Open cjyetman opened 7 years ago

cjyetman commented 7 years ago

The error/warning "It looks like Source/Target is not zero-indexed. This is required in JavaScript and so your plot may not render." seems to catch and confuse many people. This seems like something the R functions should be able to check for and fix on their own if necessary. The current version of simpleNetwork() already handles this automatically. It seems odd to me that, in the code below, the 1st one fails while the 2nd one works, the only change being that 1 was subtracted from the node ids in the Links data frame...

forceNetwork(Links = data.frame(source = c(1,1,2), target = c(2,3,4)),
             Nodes = data.frame(name = c("A", "B", "C", "D"), group = 1),
             Source = 'source', Target = 'target', 
             NodeID = 'name', Group = 'group')

forceNetwork(Links = data.frame(source = c(1,1,2) - 1, target = c(2,3,4) - 1),
             Nodes = data.frame(name = c("A", "B", "C", "D"), group = 1),
             Source = 'source', Target = 'target', 
             NodeID = 'name', Group = 'group')

Is there any reason we can't automatically re-index as below (or maybe a bit more cautiously)...

Links <- data.frame(source = c(2,2,3), target = c(3,4,5))
Links <- Links - min(unlist(Links))
christophergandrud commented 7 years ago

Makes sense to automatically reindex. Seems like this was an oversight on my part.