networkD3
networkD3 copied to clipboard
default variable names or indices for forceNetwork() arguments
The arguments in forceNetwork() that specify which variables/columns in the data frames to use could have sensible defaults. At the moment, forceNetwork() will error if all of the arguments below are not set. This is particularly annoying when your data has no concept of "Group", but you are forced to put something in there just so the function works. Defaults could be indices (preferable?), i.e. 1st column of the Links data frame is assumed to be the 'Source' column by default, or could be strings, i.e. Source = 'source'.
These are the arguments to consider making defaults for: Source, Target, Value, NodeID, Group
I like the idea of making the default indices. Though we could also do both. I.e. have the function look for named arguments first, then go with indices. Does that make sense?
I imagine it as... if the user does not set Source
in the function call, then the default is Source = 1
, so Links[[1]]
would be the source node ids. If the user sets Source = 2
in the function call, then Links[[2]]
would be the source node ids. If the user sets Source = 'srcnodes'
in the function call, then Links[['srcnodes']]
would be the source node ids.
So, it would basically be the same exact behavior as it is currently (as far as I can tell), except that you could not pass any value for the Source
argument, and it would just assume 1st column of the Links
data frame.
to put it another way, if you have this...
links <- data.frame(source = c(0,0,0,1,2), target = c(1,2,3,2,3), value = 1)
nodes <- data.frame(name = c("A","B","C","D"), group = 1)
you currently have to do this (minimally)...
forceNetwork(Links = links, Nodes = nodes, Source = "source",
Target = "target", Value = "value", NodeID = "name",
Group = "group")
but it would be nicer if you could simply do this...
forceNetwork(Links = links, Nodes = nodes)
I think the critical decision is... should the default be a reasonable index to expect, a reasonable vector/column name to expect, or do we even enable both an index and a name default, something like...
if (missing(Source)) Source <- ifelse('source' %in% names(Links), 'source', 1)
sourcenodes <- Links[[source]]
My preference is for the last option. It would give forceNetwork
a more effortless feel for users.
I think it would be helpful to have default "Group" values, especially for plotting output from Bayesian Network building package like "bnlearn". I suggest add the following to forceNetwork
function.
if (missing(Group)) { Nodes$Group <- rep(1, dim(Nodes)[1]) }