<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/comparing-dependencies-of-popular-machine-learning-packages-with-pkgnet/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://shirinsplayground.netlify.com/2018/04/pkgnet/"> Shirin's playgRound</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>When looking through the CRAN list of packages, I stumbled upon <a rel="nofollow" href="https://cran.r-project.org/web/packages/pkgnet/vignettes/pkgnet-intro.html" target="_blank">this little gem</a>:</p>
<blockquote>
<p>pkgnet is an R library designed for the analysis of R libraries! The goal of the package is to build a graph representation of a package and its dependencies.</p>
</blockquote>
<p>And I thought it would be fun to play around with it. The little analysis I ended up doing was to compare dependencies of popular machine learning packages.</p>
<hr>
<ul>
<li>I first loaded the packages:</li>
</ul>
<pre><code>library(pkgnet)
library(tidygraph)</code></pre>
<pre><code>## 
## Attache Paket: 'tidygraph'</code></pre>
<pre><code>## The following object is masked from 'package:stats':
## 
##     filter</code></pre>
<pre><code>library(ggraph)</code></pre>
<pre><code>## Lade nötiges Paket: ggplot2</code></pre>
<ul>
<li>I then created a function that will</li>
</ul>
<ol>
<li>create the package report with <code>pkgnet::CreatePackageReport</code></li>
<li>convert the edge (<code>report$DependencyReporter$edges</code>) and node (<code>report$DependencyReporter$nodes</code>) data into a graph object with <code>tidygraph::as_tbl_graph</code></li>
</ol>
<pre><code>create_pkg_graph &lt;- function(package_name, DependencyReporter = TRUE) {
  
  report &lt;- CreatePackageReport(pkg_name = package_name)
  
  if (DependencyReporter) {
    graph &lt;- as_tbl_graph(report$DependencyReporter$edges,
                      directed = TRUE,
                      nodes = as.data.frame(report$DependencyReporter$nodes))
  } else {
    graph &lt;- as_tbl_graph(report$FunctionReporter$edges,
                      directed = TRUE,
                      nodes = as.data.frame(report$FunctionReporter$nodes))
  }
  
  return(graph)
}</code></pre>
<ul>
<li>
<p>To create a vector of machine learning packages from R I looked at <a rel="nofollow" href="https://cran.r-project.org/web/views/MachineLearning.html" target="_blank">CRAN’s machine learning task view</a></p>
</li>
<li>
<p>These are the packages I ended up including:</p>
</li>
</ul>
<pre><code>pkg_list &lt;- c(&quot;caret&quot;, &quot;h2o&quot;, &quot;e1071&quot;, &quot;mlr&quot;)</code></pre>
<p><em>Note</em>: I wanted to include other packages, like <code>tensorflow</code>, <code>randomFores</code>, <code>gbm</code>, etc. but for those, <code>pkgnet</code> threw an error:</p>
<blockquote>
<p>Error in data.table::data.table(node = names(igraph::V(self$pkg_graph)), : column or argument 1 is NULL</p>
</blockquote>
<ul>
<li>Next, I ran them through my function from before and assigned them each a unique name.</li>
</ul>
<pre><code>for (pkg in pkg_list) {
  graph &lt;- create_pkg_graph(pkg)
  assign(paste0(&quot;graph_&quot;, pkg), graph)
}</code></pre>
<ul>
<li>These individual objects I combined with <a rel="nofollow" href="https://cran.r-project.org/web/packages/tidygraph/index.html" target="_blank"><code>tidygraph</code></a> and calculated node centrality as the number of outgoing edges.</li>
</ul>
<pre><code>graph &lt;- graph_caret %>% 
  graph_join(graph_h2o, by = &quot;name&quot;) %>%
  graph_join(graph_e1071, by = &quot;name&quot;) %>%
  graph_join(graph_mlr, by = &quot;name&quot;) %>%
  mutate(color = ifelse(name %in% pkg_list, &quot;a&quot;, &quot;b&quot;),
         centrality = centrality_degree(mode = &quot;out&quot;))</code></pre>
<ul>
<li>Finally, I plotted the dependency network with <a rel="nofollow" href="https://github.com/thomasp85/ggraph" target="_blank"><code>ggraph</code></a>:</li>
</ul>
<p>The bigger the node labels (package names), the higher their centrality. Seems like the more basic utilitarian packages have the highest centrality (not really a surprise…).</p>
<pre><code>graph %>%
  ggraph(layout = 'nicely') + 
    geom_edge_link(arrow = arrow()) + 
    geom_node_point() +
    geom_node_label(aes(label = name, fill = color, size = centrality), show.legend = FALSE, repel = TRUE) +
    theme_graph() +
    scale_fill_brewer(palette = &quot;Set1&quot;)</code></pre>
<p><img src="https://i0.wp.com/shirinsplayground.netlify.com/post/pkgnet_files/figure-html/graph-1.png?w=450&ssl=1" data-recalc-dims="1"></p>
<ul>
<li>Because the complete network is a bit hard to make sense of, I plotted it again with only the packages I wanted to analyze plus dependencies that had at least 1 outgoing edge; now it is easier to see shared dependencies.</li>
</ul>
<p>For example, <code>methods</code> and <code>stats</code> are dependencies of <code>caret</code>, <code>mlr</code> and <code>e1071</code> but not <code>h2o</code>, while <code>utils</code> is a dependency of all four.</p>
<pre><code>graph %>%
  filter(centrality > 1 | color == &quot;a&quot;) %>%
  ggraph(layout = 'nicely') + 
    geom_edge_link(arrow = arrow()) + 
    geom_node_point() +
    geom_node_label(aes(label = name, fill = color, size = centrality), show.legend = FALSE, repel = TRUE) +
    theme_graph() +
    scale_fill_brewer(palette = &quot;Set1&quot;)</code></pre>
<p><img src="https://i1.wp.com/shirinsplayground.netlify.com/post/pkgnet_files/figure-html/graph_subset-1.png?w=450&ssl=1" data-recalc-dims="1"></p>
<p>It would of course be interesting to analyse a bigger network with more packages. Maybe someone knows how to get these other packages to work with <code>pkgnet</code>?</p>
<pre><code>sessionInfo()</code></pre>
<pre><code>## R version 3.5.0 (2018-04-23)
## Platform: x86_64-apple-darwin15.6.0 (64-bit)
## Running under: macOS High Sierra 10.13.4
## 
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib
## 
## locale:
## [1] de_DE.UTF-8/de_DE.UTF-8/de_DE.UTF-8/C/de_DE.UTF-8/de_DE.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] bindrcpp_0.2.2  ggraph_1.0.1    ggplot2_2.2.1   tidygraph_1.1.0
## [5] pkgnet_0.2.0   
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.16         RColorBrewer_1.1-2   plyr_1.8.4          
##  [4] compiler_3.5.0       pillar_1.2.2         formatR_1.5         
##  [7] futile.logger_1.4.3  bindr_0.1.1          viridis_0.5.1       
## [10] futile.options_1.0.1 tools_3.5.0          digest_0.6.15       
## [13] viridisLite_0.3.0    gtable_0.2.0         jsonlite_1.5        
## [16] evaluate_0.10.1      tibble_1.4.2         pkgconfig_2.0.1     
## [19] rlang_0.2.0          igraph_1.2.1         ggrepel_0.7.0       
## [22] yaml_2.1.18          blogdown_0.6         xfun_0.1            
## [25] gridExtra_2.3        stringr_1.3.0        dplyr_0.7.4         
## [28] knitr_1.20           htmlwidgets_1.2      grid_3.5.0          
## [31] rprojroot_1.3-2      glue_1.2.0           data.table_1.10.4-3 
## [34] R6_2.2.2             rmarkdown_1.9        bookdown_0.7        
## [37] udunits2_0.13        tweenr_0.1.5         tidyr_0.8.0         
## [40] purrr_0.2.4          lambda.r_1.2.2       magrittr_1.5        
## [43] units_0.5-1          MASS_7.3-49          scales_0.5.0        
## [46] backports_1.1.2      mvbutils_2.7.4.1     htmltools_0.3.6     
## [49] assertthat_0.2.0     ggforce_0.1.1        colorspace_1.3-2    
## [52] labeling_0.3         stringi_1.1.7        visNetwork_2.0.3    
## [55] lazyeval_0.2.1       munsell_0.4.3</code></pre>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://shirinsplayground.netlify.com/2018/04/pkgnet/"> Shirin's playgRound</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/-uN9sS-mUIw" height="1"></a></div>