networkD3 icon indicating copy to clipboard operation
networkD3 copied to clipboard

re-index nodes from 0 if necessary

Open cjyetman opened this issue 8 years ago • 1 comments

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))

cjyetman avatar May 02 '17 08:05 cjyetman

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

christophergandrud avatar May 03 '17 15:05 christophergandrud