title: R: how to make a networkD3 network reactive to subsetting of the data
body: <p>I'm working with the <code>networkD3</code> package, and I'm unable to make some sub-networks with my data.</p>
  
  <p>I have a dataset made in this way (<code>MisNodes</code> and <code>MisLinks</code> are provided by the package itself):</p>
  
  <pre><code>library(networkD3)
  
  data("MisNodes")
  head(MisNodes)
  #              name group size
  # 1          Myriel     1   15
  # 2        Napoleon     1   20
  # 3 Mlle.Baptistine     1   23
  # 4    Mme.Magloire     1   30
  # 5    CountessdeLo     1   11
  # 6        Geborand     1    9
  
  data("MisLinks")
  head(MisLinks)
  #   source target value
  # 1      1      0     1
  # 2      2      0     8
  # 3      3      0    10
  # 4      3      2     6
  # 5      4      0     1
  # 6      5      0     1
  </code></pre>
  
  <p>The full network is this:</p>
  
  <pre><code>forceNetwork(
    Links  = MisLinks, Nodes   = MisNodes,
    Source = "source", Target  = "target",
    Value  = "value",  NodeID  = "name",
    Group  = "group",  opacity = 1)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/IpsHs.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IpsHs.png" alt="enter image description here"></a></p>
  
  <p>Now, I'd like to select one of the group, and see only the nodes of that group, and the links that have as source the nodes in that group:</p>
  
  <p>So I tried this:</p>
  
  <pre><code># here the group
  k &lt;-c(1)
  
  # add id, to subset well
  MisNodes$id &lt;- rownames(MisNodes)
  
  # select the desired nodes
  nodes &lt;- (MisNodes[MisNodes$group %in% k,])
  
  # select the links that have as source the desired nodes
  links &lt;- (MisLinks[rownames(MisLinks) %in% nodes$id,])   
  
  # rownames from 0 
  rownames(nodes) &lt;- 1:nrow(nodes)-1 
  
  # indexing from 0 to max
  links$source_ &lt;-match(links$source, sort(unique(links$source)))-1
  links$target_ &lt;-match(links$target, sort(unique(links$target)))-1
  </code></pre>
  
  <p>But the result is not correct, due that alone point should be linked to the central node.</p>
  
  <p><a href="https://i.stack.imgur.com/mOUOa.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mOUOa.png" alt="enter image description here"></a></p>
  
  <p>Looking at the links:</p>
  
  <pre><code>links
     source target value source_ target_
  1       1      0     1       0       0
  2       2      0     8       1       0
  3       3      0    10       2       0
  4       3      2     6       2       1
  5       4      0     1       3       0
  6       5      0     1       4       0
  7       6      0     1       5       0
  8       7      0     1       6       0
  9       8      0     2       7       0
  10      9      0     1       8       0
  </code></pre>
  
  <p>Seems that the sources start from 1, but target from 0, but removing the -1, it works for the first group, but not from the others. Also the total using <code>k &lt;-c(1,2,3,4,5,6,7,8,9,10)</code> is not correct. How could I make the network reactive correctly to subset of the data?</p>