R, D3js and SNA Course

[This article was first published on Jkunst - R category, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Update 2015-11-09: This is migration from an old post.

I took the SNA course by Lada Adamic in coursera.
It’s a super interesting course. In fact, I was using the networks only how a visualization tool, and that is
what it make me little bit embarrassing because there are more, a lot of more. You can detect communities, know
more centric nodes and a lot of other information. So, there are a lot of reasons to look the course

By other hand I like the d3 javascript library. Recently I was learning javascript, so I decided make a very
little app to keep learning this library and show differents measures of centrality for each node in a set
of 4 toy networks and see these measures by size, color or a label

Now, the R code to make the data:

<span class="n">rm</span><span class="p">(</span><span class="n">list</span> <span class="o">=</span> <span class="n">ls</span><span class="p">())</span>
<span class="c1">##### Load Packages ####
</span><span class="n">library</span><span class="p">(</span><span class="s2">"sna"</span><span class="p">)</span> 
<span class="c1"># for build a block diagonal matrix
</span><span class="n">library</span><span class="p">(</span><span class="s2">"Matrix"</span><span class="p">)</span>   
<span class="c1"># (https://stat.ethz.ch/pipermail/r-help/2007-June/133875.html)
</span><span class="n">library</span><span class="p">(</span><span class="s2">"reldist"</span><span class="p">)</span>  
<span class="n">library</span><span class="p">(</span><span class="s2">"plyr"</span><span class="p">)</span>
<span class="n">library</span><span class="p">(</span><span class="s2">"rjson"</span><span class="p">)</span>

<span class="c1">##### Functions ####
</span><span class="n">degree_sna</span> <span class="o"><-</span> <span class="k">function</span><span class="p">(</span><span class="n">net</span><span class="p">,</span> <span class="n">norm</span> <span class="o">=</span> <span class="n">TRUE</span><span class="p">,</span> <span class="k">...</span><span class="p">){</span>
  <span class="n">degree</span><span class="p">(</span><span class="n">net</span><span class="p">,</span> <span class="k">...</span><span class="p">)</span><span class="o">/</span><span class="m">2</span><span class="o">/</span><span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="n">norm</span><span class="p">)</span> <span class="n">ncol</span><span class="p">(</span><span class="n">net</span><span class="p">)</span> <span class="o">-</span> <span class="m">1</span> <span class="k">else</span> <span class="m">1</span><span class="p">)</span>
<span class="p">}</span>

<span class="n">betweenness_sna</span> <span class="o"><-</span> <span class="k">function</span><span class="p">(</span><span class="n">net</span><span class="p">,</span> <span class="n">norm</span> <span class="o">=</span> <span class="n">FALSE</span><span class="p">,</span> <span class="k">...</span><span class="p">){</span>
  <span class="n">n</span> <span class="o"><-</span> <span class="n">ncol</span><span class="p">(</span><span class="n">net</span><span class="p">)</span>
  <span class="n">betweenness</span><span class="p">(</span><span class="n">net</span><span class="p">,</span> <span class="k">...</span><span class="p">)</span><span class="o">/</span><span class="m">2</span><span class="o">/</span><span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="n">norm</span><span class="p">)</span> <span class="p">(</span><span class="n">n</span> <span class="o">-</span> <span class="m">1</span><span class="p">)</span><span class="o">*</span><span class="p">(</span><span class="n">n</span> <span class="o">-</span> <span class="m">2</span><span class="p">)</span><span class="o">/</span><span class="m">2</span> <span class="k">else</span> <span class="m">1</span><span class="p">)</span>
<span class="p">}</span>

<span class="c1">##### Networks ####
</span><span class="n">net.butterfly</span> <span class="o"><-</span> <span class="n">matrix</span><span class="p">(</span><span class="n">c</span><span class="p">(</span><span class="m">0</span><span class="p">,</span><span class="m">1</span><span class="p">,</span><span class="m">1</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span>
                          <span class="m">1</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">1</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span>
                          <span class="m">1</span><span class="p">,</span><span class="m">1</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">1</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span>
                          <span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">1</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">1</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span>
                          <span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">1</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">1</span><span class="p">,</span><span class="m">1</span><span class="p">,</span>
                          <span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">1</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">1</span><span class="p">,</span>
                          <span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">1</span><span class="p">,</span><span class="m">1</span><span class="p">,</span><span class="m">0</span><span class="p">),</span>
                        <span class="n">byrow</span> <span class="o">=</span> <span class="n">TRUE</span><span class="p">,</span> <span class="n">nrow</span> <span class="o">=</span> <span class="m">7</span><span class="p">)</span>

<span class="n">net.star</span> <span class="o"><-</span> <span class="n">matrix</span><span class="p">(</span><span class="n">c</span><span class="p">(</span><span class="m">0</span><span class="p">,</span><span class="m">1</span><span class="p">,</span><span class="m">1</span><span class="p">,</span><span class="m">1</span><span class="p">,</span><span class="m">1</span><span class="p">,</span><span class="m">1</span><span class="p">,</span>
                     <span class="m">1</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span>
                     <span class="m">1</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span>
                     <span class="m">1</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span>
                     <span class="m">1</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span>
                     <span class="m">1</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">),</span>
                   <span class="n">byrow</span> <span class="o">=</span> <span class="n">TRUE</span><span class="p">,</span> <span class="n">nrow</span> <span class="o">=</span> <span class="m">6</span><span class="p">)</span>

<span class="n">net.line</span> <span class="o"><-</span> <span class="n">matrix</span><span class="p">(</span><span class="n">c</span><span class="p">(</span><span class="m">0</span><span class="p">,</span><span class="m">1</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span>
                     <span class="m">1</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">1</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span>
                     <span class="m">0</span><span class="p">,</span><span class="m">1</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">1</span><span class="p">,</span><span class="m">0</span><span class="p">,</span>
                     <span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">1</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">1</span><span class="p">,</span>
                     <span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">1</span><span class="p">,</span><span class="m">0</span><span class="p">),</span>
                   <span class="n">byrow</span> <span class="o">=</span> <span class="n">TRUE</span><span class="p">,</span> <span class="n">nrow</span> <span class="o">=</span> <span class="m">5</span><span class="p">)</span>

<span class="n">net.circular</span> <span class="o"><-</span> <span class="n">matrix</span><span class="p">(</span><span class="n">c</span><span class="p">(</span><span class="m">0</span><span class="p">,</span><span class="m">1</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">1</span><span class="p">,</span>
                         <span class="m">1</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">1</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span>
                         <span class="m">0</span><span class="p">,</span><span class="m">1</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">1</span><span class="p">,</span><span class="m">0</span><span class="p">,</span>
                         <span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">1</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">1</span><span class="p">,</span>
                         <span class="m">1</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">1</span><span class="p">,</span><span class="m">0</span><span class="p">),</span>
                       <span class="n">byrow</span> <span class="o">=</span> <span class="n">TRUE</span><span class="p">,</span> <span class="n">nrow</span> <span class="o">=</span> <span class="m">5</span><span class="p">)</span>

<span class="n">nets</span> <span class="o"><-</span> <span class="n">list</span><span class="p">(</span><span class="n">net.butterfly</span><span class="p">,</span> <span class="n">net.star</span><span class="p">,</span> <span class="n">net.line</span><span class="p">,</span> <span class="n">net.circular</span><span class="p">)</span>
<span class="n">net.all</span> <span class="o"><-</span> <span class="n">as.matrix</span><span class="p">(</span><span class="n">bdiag</span><span class="p">(</span><span class="n">net.butterfly</span><span class="p">,</span> <span class="n">net.star</span><span class="p">,</span> <span class="n">net.line</span><span class="p">,</span> <span class="n">net.circular</span><span class="p">))</span>


<span class="c1">##### Plots ####
</span><span class="n">gplot</span><span class="p">(</span><span class="n">net.butterfly</span><span class="p">,</span> <span class="n">displaylabels</span> <span class="o">=</span> <span class="n">TRUE</span><span class="p">,</span> <span class="n">usearrows</span> <span class="o">=</span> <span class="n">FALSE</span><span class="p">)</span>

plot of chunk unnamed-chunk-1

<span class="n">gplot</span><span class="p">(</span><span class="n">net.star</span><span class="p">,</span> <span class="n">displaylabels</span> <span class="o">=</span> <span class="n">TRUE</span><span class="p">,</span> <span class="n">usearrows</span> <span class="o">=</span> <span class="n">FALSE</span><span class="p">)</span>

plot of chunk unnamed-chunk-1

<span class="n">gplot</span><span class="p">(</span><span class="n">net.line</span><span class="p">,</span> <span class="n">displaylabels</span> <span class="o">=</span> <span class="n">TRUE</span><span class="p">,</span> <span class="n">usearrows</span> <span class="o">=</span> <span class="n">FALSE</span><span class="p">)</span>

plot of chunk unnamed-chunk-1

<span class="n">gplot</span><span class="p">(</span><span class="n">net.circular</span><span class="p">,</span> <span class="n">displaylabels</span> <span class="o">=</span> <span class="n">TRUE</span><span class="p">,</span> <span class="n">usearrows</span> <span class="o">=</span> <span class="n">FALSE</span><span class="p">)</span>

plot of chunk unnamed-chunk-1

<span class="n">gplot</span><span class="p">(</span><span class="n">net.all</span><span class="p">,</span> <span class="n">usearrows</span> <span class="o">=</span> <span class="n">FALSE</span><span class="p">,</span>
      <span class="n">label</span> <span class="o">=</span> <span class="n">unlist</span><span class="p">(</span><span class="n">llply</span><span class="p">(</span><span class="n">nets</span><span class="p">,</span> <span class="n">degree_sna</span><span class="p">,</span> <span class="n">norm</span> <span class="o">=</span> <span class="n">FALSE</span><span class="p">)))</span>

plot of chunk unnamed-chunk-1

<span class="c1">#### Indicators ####
# Degrees for each node of each network
</span><span class="n">llply</span><span class="p">(</span><span class="n">nets</span><span class="p">,</span> <span class="n">degree_sna</span><span class="p">)</span>
## [[1]]
## [1] 0.333 0.333 0.500 0.333 0.500 0.333 0.333
## 
## [[2]]
## [1] 1.0 0.2 0.2 0.2 0.2 0.2
## 
## [[3]]
## [1] 0.25 0.50 0.50 0.50 0.25
## 
## [[4]]
## [1] 0.5 0.5 0.5 0.5 0.5
<span class="n">llply</span><span class="p">(</span><span class="n">nets</span><span class="p">,</span> <span class="n">degree_sna</span><span class="p">,</span> <span class="n">norm</span> <span class="o">=</span> <span class="n">FALSE</span><span class="p">)</span>
## [[1]]
## [1] 2 2 3 2 3 2 2
## 
## [[2]]
## [1] 5 1 1 1 1 1
## 
## [[3]]
## [1] 1 2 2 2 1
## 
## [[4]]
## [1] 2 2 2 2 2
<span class="c1"># Differences beetween degree for nodes in each network
</span><span class="n">laply</span><span class="p">(</span><span class="n">nets</span><span class="p">,</span> <span class="k">function</span><span class="p">(</span><span class="n">net</span><span class="p">){</span> <span class="n">gini</span><span class="p">(</span><span class="n">degree_sna</span><span class="p">(</span><span class="n">net</span><span class="p">))</span> <span class="p">})</span>
## [1] 0.0893 0.3333 0.1500 0.0000
<span class="n">laply</span><span class="p">(</span><span class="n">nets</span><span class="p">,</span> <span class="k">function</span><span class="p">(</span><span class="n">net</span><span class="p">){</span>   <span class="n">sd</span><span class="p">(</span><span class="n">degree_sna</span><span class="p">(</span><span class="n">net</span><span class="p">))</span> <span class="p">})</span>
## [1] 0.0813 0.3266 0.1369 0.0000
<span class="c1"># Centralization coefficient $C_D$
</span><span class="n">laply</span><span class="p">(</span><span class="n">nets</span><span class="p">,</span> <span class="n">centralization</span><span class="p">,</span> <span class="n">degree</span><span class="p">)</span>
## [1] 0.167 1.000 0.167 0.000
<span class="c1"># Betweenness
</span><span class="n">llply</span><span class="p">(</span><span class="n">nets</span><span class="p">,</span> <span class="n">betweenness_sna</span><span class="p">)</span>
## [[1]]
## [1] 0 0 8 9 8 0 0
## 
## [[2]]
## [1] 10  0  0  0  0  0
## 
## [[3]]
## [1] 0 3 4 3 0
## 
## [[4]]
## [1] 1 1 1 1 1
<span class="n">llply</span><span class="p">(</span><span class="n">nets</span><span class="p">,</span> <span class="n">betweenness_sna</span><span class="p">,</span> <span class="n">norm</span> <span class="o">=</span> <span class="n">TRUE</span><span class="p">)</span>
## [[1]]
## [1] 0.000 0.000 0.533 0.600 0.533 0.000 0.000
## 
## [[2]]
## [1] 1 0 0 0 0 0
## 
## [[3]]
## [1] 0.000 0.500 0.667 0.500 0.000
## 
## [[4]]
## [1] 0.167 0.167 0.167 0.167 0.167
<span class="c1"># Closeness
</span><span class="n">llply</span><span class="p">(</span><span class="n">nets</span><span class="p">,</span> <span class="n">closeness</span><span class="p">)</span>
## [[1]]
## [1] 0.400 0.400 0.545 0.600 0.545 0.400 0.400
## 
## [[2]]
## [1] 1.000 0.556 0.556 0.556 0.556 0.556
## 
## [[3]]
## [1] 0.400 0.571 0.667 0.571 0.400
## 
## [[4]]
## [1] 0.667 0.667 0.667 0.667 0.667
<span class="c1"># Eigenvector Centrality
</span><span class="n">llply</span><span class="p">(</span><span class="n">nets</span><span class="p">,</span> <span class="n">evcent</span><span class="p">)</span>
## [[1]]
## [1] 0.335 0.335 0.450 0.384 0.450 0.335 0.335
## 
## [[2]]
## [1] 0.408 0.408 0.408 0.408 0.408 0.408
## 
## [[3]]
## [1] 0.309 0.463 0.617 0.463 0.309
## 
## [[4]]
## [1] 0.447 0.447 0.447 0.447 0.447
<span class="c1">#### Consolidate Data ####
</span><span class="n">names</span> <span class="o"><-</span> <span class="n">paste</span><span class="p">(</span><span class="n">rep</span><span class="p">(</span><span class="m">1</span><span class="o">:</span><span class="n">length</span><span class="p">(</span><span class="n">nets</span><span class="p">),</span> <span class="n">laply</span><span class="p">(</span><span class="n">nets</span><span class="p">,</span> <span class="n">ncol</span><span class="p">)),</span>
               <span class="n">unlist</span><span class="p">(</span><span class="n">llply</span><span class="p">(</span><span class="n">nets</span><span class="p">,</span> <span class="k">function</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="m">1</span><span class="o">:</span><span class="n">ncol</span><span class="p">(</span><span class="n">x</span><span class="p">))),</span> <span class="n">sep</span> <span class="o">=</span> <span class="s2">"_"</span><span class="p">)</span>

<span class="n">colnames</span><span class="p">(</span><span class="n">net.all</span><span class="p">)</span> <span class="o"><-</span> <span class="n">names</span>
<span class="n">rownames</span><span class="p">(</span><span class="n">net.all</span><span class="p">)</span> <span class="o"><-</span> <span class="n">names</span>

<span class="n">links</span> <span class="o"><-</span> <span class="n">ldply</span><span class="p">(</span><span class="n">names</span><span class="p">,</span> <span class="k">function</span><span class="p">(</span><span class="n">name</span><span class="p">){</span>
  <span class="c1"># name <- sample(names, size = 1)
</span>  <span class="c1"># name <- names[1]
</span>  <span class="n">data.frame</span><span class="p">(</span><span class="n">source</span> <span class="o">=</span> <span class="n">which</span><span class="p">(</span><span class="n">names</span> <span class="o">==</span> <span class="n">name</span><span class="p">)</span> <span class="o">-</span> <span class="m">1</span><span class="p">,</span> 
             <span class="n">target</span> <span class="o">=</span> <span class="n">which</span><span class="p">(</span><span class="n">net.all</span><span class="p">[</span><span class="n">name</span><span class="p">,]</span> <span class="o">==</span> <span class="m">1</span><span class="p">)</span> <span class="o">-</span> <span class="m">1</span><span class="p">)</span>
<span class="p">})</span>

<span class="n">nodes</span> <span class="o"><-</span> <span class="n">data.frame</span><span class="p">(</span><span class="n">name</span> <span class="o">=</span> <span class="n">names</span><span class="p">)</span>
<span class="n">nodes</span><span class="o">$</span><span class="n">degree_norm</span> <span class="o"><-</span> <span class="n">unlist</span><span class="p">(</span><span class="n">llply</span><span class="p">(</span><span class="n">nets</span><span class="p">,</span> <span class="n">degree_sna</span><span class="p">))</span>
<span class="n">nodes</span><span class="o">$</span><span class="n">degree</span> <span class="o"><-</span> <span class="n">unlist</span><span class="p">(</span><span class="n">llply</span><span class="p">(</span><span class="n">nets</span><span class="p">,</span> <span class="n">degree_sna</span><span class="p">,</span> <span class="n">norm</span> <span class="o">=</span> <span class="n">FALSE</span><span class="p">))</span>
<span class="n">nodes</span><span class="o">$</span><span class="n">betweenness</span> <span class="o"><-</span> <span class="n">unlist</span><span class="p">(</span><span class="n">llply</span><span class="p">(</span><span class="n">nets</span><span class="p">,</span> <span class="n">betweenness_sna</span><span class="p">))</span>
<span class="n">nodes</span><span class="o">$</span><span class="n">betweenness_norm</span> <span class="o"><-</span> <span class="n">unlist</span><span class="p">(</span><span class="n">llply</span><span class="p">(</span><span class="n">nets</span><span class="p">,</span> <span class="n">betweenness_sna</span><span class="p">,</span> <span class="n">norm</span> <span class="o">=</span> <span class="n">TRUE</span><span class="p">))</span>
<span class="n">nodes</span><span class="o">$</span><span class="n">closeness</span> <span class="o"><-</span> <span class="n">unlist</span><span class="p">(</span><span class="n">llply</span><span class="p">(</span><span class="n">nets</span><span class="p">,</span> <span class="n">closeness</span><span class="p">))</span>
<span class="n">nodes</span><span class="o">$</span><span class="n">eigen_vector_centrality</span> <span class="o"><-</span> <span class="n">unlist</span><span class="p">(</span><span class="n">llply</span><span class="p">(</span><span class="n">nets</span><span class="p">,</span> <span class="n">evcent</span><span class="p">))</span>

<span class="c1">#### Exporting Data ####
</span><span class="n">nodes_json</span> <span class="o"><-</span> <span class="n">adply</span><span class="p">(</span><span class="n">nodes</span><span class="p">,</span> <span class="m">1</span><span class="p">,</span> <span class="n">toJSON</span> <span class="p">)</span><span class="o">$</span><span class="n">V1</span>
<span class="n">nodes_json</span> <span class="o"><-</span> <span class="n">paste</span><span class="p">(</span><span class="s2">" "nodes" : ["</span><span class="p">,</span> <span class="n">paste</span><span class="p">(</span><span class="s2">"n"</span><span class="p">,</span> <span class="n">nodes_json</span><span class="p">,</span> <span class="n">collapse</span> <span class="o">=</span> <span class="s2">", "</span><span class="p">),</span> <span class="s2">"n]"</span><span class="p">)</span>

<span class="n">links_json</span> <span class="o"><-</span> <span class="n">adply</span><span class="p">(</span><span class="n">links</span><span class="p">,</span> <span class="m">1</span><span class="p">,</span> <span class="n">toJSON</span><span class="p">)</span><span class="o">$</span><span class="n">V1</span>
<span class="n">links_json</span> <span class="o"><-</span> <span class="n">paste</span><span class="p">(</span><span class="s2">" "links" : ["</span><span class="p">,</span> <span class="n">paste</span><span class="p">(</span><span class="s2">"n"</span><span class="p">,</span> <span class="n">links_json</span><span class="p">,</span> <span class="n">collapse</span> <span class="o">=</span> <span class="s2">", "</span><span class="p">),</span> <span class="s2">"n]"</span><span class="p">)</span>

<span class="n">data_json</span> <span class="o"><-</span> <span class="n">paste</span><span class="p">(</span><span class="s2">"{n"</span><span class="p">,</span> <span class="n">nodes_json</span><span class="p">,</span> <span class="s2">"n,n"</span><span class="p">,</span> <span class="n">links_json</span><span class="p">,</span> <span class="s2">"}"</span><span class="p">)</span>
<span class="c1"># write(data_json, "data.json")
</span>

You can fork the repo from here.

To leave a comment for the author, please follow the link and comment on their blog: Jkunst - R category.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)