<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/introducing-tidygraph/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.data-imaginist.com/2017/Introducing-tidygraph/"> Data Imaginist - R posts</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I’m very pleased to announce that my new package <code>tidygraph</code> is now<br>
<a rel="nofollow" href="https://cran.r-project.org/package=tidygraph" target="_blank">available on CRAN</a>. As the name<br>
suggests, <code>tidygraph</code> is an entry into the tidyverse that provides a tidy<br>
framework for all things relational (networks/graphs, trees, etc.). <code>tidygraph</code><br>
is a relatively big package in terms of exported functions (280 exported<br>
symbols) so all functions will not be covered in this release note. I will<br>
however provide an overview of all the areas that <code>tidygraph</code>touches upon so you<br>
should have a pretty good grasp on what the package can do for you.</p>
<h2>Tidy network data?</h2>
<p>There’s a discrepancy between relational data and the tidy data idea, in that<br>
relational data cannot in any meaningful way be encoded as a single tidy<br>
data frame. On the other hand, both node and edge data by itself fits very well<br>
within the tidy concept as each node and edge is, in a sense, a single<br>
observation. Thus, a close approximation of tidyness for relational data is two<br>
tidy data frames, one describing the node data and one describing the edge data.</p>
<h3>The tbl_graph object</h3>
<p>Underneath the hood of <code>tidygraph</code> lies the well-oiled machinery of igraph,<br>
ensuring efficient graph manipulation. Rather than keeping the node and edge<br>
data in a list and creating <code>igraph</code> objects on the fly when needed, <code>tidygraph</code><br>
subclasses <code>igraph</code> with the <code>tbl_graph</code> class and simply exposes it in a tidy<br>
manner. This ensures that all your beloved algorithms that expects <code>igraph</code><br>
objects still works with <code>tbl_graph</code> objects. Further, <code>tidygraph</code> is very<br>
careful not to override any of <code>igraph</code>s exports so the two packages can coexist<br>
quite happily.</p>
<p>To underline the tidyness of the <code>tbl_graph</code> class the print method shows the<br>
object as two tibbles along with additional network information.</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>tidygraph</span><span>)</span><span>
</span><span>create_ring</span><span>(</span><span>10</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">#> # A tbl_graph: 10 nodes and 10 edges
#> #
#> # An undirected simple graph with 1 component
#> #
#> # Node Data: 10 x 0 (active)
#> #
#> # Edge Data: 10 x 2
#>    from    to
#>   </code></pre></figure>