---
title: "CRAN top 20s and interactive R package directives and collaboration networks"
author: "[Ioannis Kosmidis](http://www.ikosmidis.com)"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{CRAN top 20s and interactive package and collaboration networks}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 6,
  fig.height = 6
)
```


# **cranly**
[**cranly**](https://github.com/ikosmidis/cranly) provides core visualizations and summaries for the CRAN package database. It is aimed mainly as an analytics tool for developers to keep track of their CRAN packages and profiles, as well as those of others, which, at least for me, is proving harder and harder as the CRAN ecosystem grows.

The package provides comprehensive methods for cleaning up and organizing the information in the CRAN package database, for building package directives networks (depends, imports, suggests, enhances) and collaboration networks, and for computing summaries and producing interactive visualizations from the resulting networks. Network visualization is through the [**visNetwork**](https://CRAN.R-project.org/package=visNetwork) package. The package also provides functions to coerce the networks to igraph <https://CRAN.R-project.org/package=igraph> objects for further analyses and modelling.

This vignette is a tour to the current capabilities in [**cranly**](https://github.com/ikosmidis/cranly).

# Preparing today's CRAN package database

Download today's CRAN database
```{r}
library("cranly")
p_db <- tools::CRAN_package_db()
```

Next we need to clean and organize author names, depends, imports, suggests, enhances
```{r}
package_db <- clean_CRAN_db(p_db)
```

The resulting dataset carries the timestamp of when it was put together, which helps keeping track of when the data import has taken place and will be helpful in future versions when dynamic analyses and visualization methods are implemented.
```{r}
attr(package_db, "timestamp")
```

# Network of package directives

We can now extract edges and nodes for the CRAN package directives network by simply doing
```{r}
package_network <- build_network(package_db)
```
and compute various statistics for the package network
```{r}
## Global package network statistics
package_summaries <- summary(package_network)
```

The `package_summaries` object can now be used for finding the top-20
packages according to various statistics
```{r}
plot(package_summaries, according_to = "n_imported_by", top = 20)
plot(package_summaries, according_to = "page_rank", top = 20)
plot(package_summaries, according_to = "betweenness", top = 20)
plot(package_summaries, according_to = "n_enhances", top = 20)
plot(package_summaries, according_to = "n_authors", top = 20)
plot(package_summaries, according_to = "n_imports", top = 20)
```

The sub-network for my packages can be found using the extractor function `package_of` which use exact matching by default
```{r}
my_packages <- package_by(package_network, "Ioannis Kosmidis")
my_packages
```
We can now get an interactive visualization of the sub-network for my packages using
```{r}
plot(package_network, package = my_packages, title = TRUE, legend = TRUE)
```
You can hover over the nodes and the edges to get package-specific information and links to the package pages.

In order to *focus only on optional packages* (i.e. exclude base and recommended packages), we do
```{r}
optional_packages <- subset(package_network, recommended = FALSE, base = FALSE)
optional_summary <- summary(optional_packages)
plot(optional_summary, top = 30, according_to = "n_imported_by")
```

# CRAN collaboration network

Next let's build the CRAN collaboration network
```{r}
author_network <- build_network(object = package_db, perspective = "author")
```

Statistics for the collaboration network can be computed using the `summary` method as we did for package directives.
```{r}
author_summaries <- summary(author_network)
```

The top-20 collaborators according to various network statistics are
```{r}
plot(author_summaries, according_to = "n_packages", top = 20)
plot(author_summaries, according_to = "page_rank", top = 20)
plot(author_summaries, according_to = "betweenness", top = 20)
```

The R Core's collaboration sub-network is
```{r}
plot(author_network, author = "R Core")
```
and my (small but valuable to me!) collaboration sub-network is
```{r}
plot(author_network, author = "Kosmidis", exact = FALSE)
```
