vignettes/pkgnet-intro.Rmd
pkgnet-intro.Rmd
pkgnet
is an R package designed for the analysis of R packages! The goal of the package is to build graph representations of a package’s various types of dependencies. This can inform a variety of activities, including:
Below is a brief tour of pkgnet
and its features.
pkgnet
represents aspects of R packages as graphs. The two default reporters, which we will discuss in this vignette, model their respective aspects as directed graphs: a package’s dependencies on other packages, and the interdependencies of functions within a package. Before we look at the output of pkgnet
, here are few core concepts to keep in mind.
Units of the analysis are represented as nodes, and their dependency relationships are represented as edges (a.k.a. arcs or arrows). In pkgnet
, the nodes could be functions in the package you are examining, or other packages that the package depends on. The direction of edges point in the direction of dependency—the tail node depends on the head node.1
In the example dependency graph above:
Following the direction of the edges allows you to figure out the dependencies of a node—the other nodes that it depends on. On the flip side, tracing the edges backwards allows you to figure out the reverse dependencies (i.e., dependents) of a node—the other nodes that depend on it.
pkgnet
can analyze any R package locally installed. (Run installed.packages()
to see the full list of packages installed on your system.) For this example, let’s say we are analyzing a custom built package, baseballstats
.
To analyze baseballstats
, run the following two lines of code:
library(pkgnet)
report1 <- CreatePackageReport(pkg_name = "baseballstats")
That’s it! You have generated a lot of valuable information with that one call for an installed package.
However, if the full source repository for the package is available on your system, you can supplement this report with other information such as code coverage from covr. To do so, specify the path to the repository in CreatePackageReport
.
library(pkgnet)
CreatePackageReport(
report2 <-pkg_name = "baseballstats"
pkg_path = <path to the repo>
, )
CreatePackageReport
has written an HTML report with the pertinent information, and it also returned a list object with the same information and more.
The location of the HTML report is specified in the messages in the terminal.
This report has three sections:
Each section has helpful tables and visuals.
As a sample, here’s how the Function Network Visualization looks for baseballstats
:
Check out the full HTML report for more results
The CreatePackageReport()
function returns a list with three items:
Each items contains information visible in the report and more. We can use this information for a more detailed analysis of the results and/or more easily incorporate pkgnet
results into other R processes.
Here are a few notable items available within the list object:
Both the DependencyReporter
and the FunctionReporter
contain metrics about their package dependencies or functions (a.k.a network nodes) in a nodes
table.
dim(report2$FunctionReporter$nodes)
#> [1] 5 14
names(report2$FunctionReporter$nodes)
#> [1] "node" "type" "isExported"
#> [4] "coveredLines" "totalLines" "coverageRatio"
#> [7] "meanCoveragePerLine" "filename" "betweenness"
#> [10] "outDegree" "inDegree" "numRecursiveDeps"
#> [13] "numRecursiveRevDeps" "pageRank"
Note, a few of these metrics provided by default are from the field of Network Theory. You can leverage the Network Object described below to derive many more.
Both the DependencyReporter
and the FunctionReporter
contain graph-level measures based on their network structure in a network_measures
list.
report2$FunctionReporter$network_measures
#> $packageTestCoverage.mean
#> [1] 0.1
#>
#> $packageTestCoverage.betweenessWeightedMean
#> [1] 0
#>
#> $graphOutDegree
#> [1] 0.3
#>
#> $graphInDegree
#> [1] 0.3
#>
#> $graphBetweenness
#> [1] 0.03125
Both the DependencyReporter
and the FunctionReporter
have an object called pkg_graph
that contains the graph model of their respective networks. This object has methods to calculate additional node-level and graph-level measures. It is powered by igraph, and the igraph object itself is directly accessible with pkg_graph$igraph
.
report2$FunctionReporter$pkg_graph$node_measures(c('hubScore', 'authorityScore'))
#> node hubScore authorityScore
#> 1: OPS 1 0
#> 2: at_bats 0 1
#> 3: batting_avg 1 1
#> 4: on_base_pct 0 0
#> 5: slugging_avg 1 1
report2$FunctionReporter$pkg_graph$igraph
#> IGRAPH f3f0cd3 DN-- 5 4 --
#> + attr: name (v/c)
#> + edges from f3f0cd3 (vertex names):
#> [1] slugging_avg->at_bats batting_avg ->at_bats
#> [3] OPS ->slugging_avg OPS ->batting_avg
With the reports and objects produced by pkgnet
by default, there is plenty to inform us on the inner workings of an R package. However, we may want to know MORE! Since the igraph objects are available, we can leverage those graphs for further analysis.
In this section, let’s examine a larger R package, such as lubridate.
If you would like to follow along with the examples in this section, run these commands in your terminal to download and install lubridate
2.
# Create a temporary workspace
mkdir -p ~/pkgnet_example && cd ~/pkgnet_example
# Grab the lubridate source code
git clone https://github.com/tidyverse/lubridate
cd lubridate
# If you want the examples to match exactly
git reset --hard 9797d69abe1574dd89310c834e52d358137669b8
# Install it
R CMD install .
Let’s examine lubridate
’s functions through the lens of each function’s total number of dependents (i.e., the other functions that depend on it) and its code’s unit test coverage. In our graph model for the FunctionReporter
, the subgraph of paths leading into a given node is the set of functions that directly or indirectly depend on the function that node represents.
# Run pkgnet
library(pkgnet)
report2 <- CreatePackageReport(
pkg_name = "lubridate"
, pkg_path = "~/pkgnet_example/lubridate"
)
# Extract Nodes Table
funcNodes <- report2$FunctionReporter$nodes
# List Coverage For Most Depended-on Functions
mostRef <- funcNodes[order(numRecursiveRevDeps, decreasing = TRUE),
.(node, numRecursiveRevDeps, coverageRatio, totalLines)
][1:10]
#> node numRecursiveRevDeps coverageRatio totalLines
#> 1: month 81 1 1
#> 2: tz 79 1 1
#> 3: reclass_date 68 1 1
#> 4: date 67 1 1
#> 5: is.Date 60 1 1
#> 6: is.POSIXt 57 1 1
#> 7: wday 56 1 1
#> 8: is.POSIXct 55 1 1
#> 9: .deprecated 55 0 10
#> 10: as_date 52 1 1
Inspecting results such as these can help an R package developer decide which function to cover with unit tests next.
In this case, check_duration
, one of the most depended-on functions (either directly or indirectly), is not covered by unit tests. However, it appears to be a simple one line function that may not be necessary to cover in unit testing. check_interval
, on the other hand, might benefit from some unit test coverage as it is a larger, uncovered function with a similar number of dependencies.
Looking at that same large package, let’s say we want to explore options for consolidating functions. One approach might be to explore consolidating functions that share the same dependencies. In that case, we could use the igraph
object to highlight functions with the same out-neighborhood via Jaccard similarity.
# Get igraph object
funcGraph <- report2$FunctionReporter$pkg_graph$igraph
funcNames <- igraph::vertex_attr(funcGraph, name = "name")
# Jaccard Similarity
sim <- igraph::similarity(graph = funcGraph
, mode = "out"
, method = "jaccard")
diag(sim) <- 0
sim[sim < 1] <- 0
simGraph <- igraph::graph_from_adjacency_matrix(adjmatrix = sim, mode = "undirected")
# Find groups with same out-neighbors (similarity == 1)
sameDeps <- igraph::max_cliques(graph = simGraph
, min = 2
)
# Write results
for (i in seq_along(sameDeps)) {
cat(paste0("Group ", i, ": "))
cat(paste(funcNames[as.numeric(sameDeps[[i]])], collapse = ", "))
cat("\n")
}
#> Group 1: divisible_period, make_date
#> Group 2: parse_date_time2, fast_strptime
#> Group 3: .deprecated_fun, .deprecated_arg
#> Group 4: stamp_date, stamp_time
#> Group 5: epiweek, isoweek
#> Group 6: ms, hm
#> Group 7: quarter, semester
#> Group 8: am, .roll_hms
#> Group 9: modulo_interval_by_duration, modulo_interval_by_period
#> Group 10: .difftime_from_pieces, .duration_from_units
#> Group 11: divide_period_by_period, xtfrm.Period
#> Group 12: int_diff, %--%
#> Group 13: isoyear, epiyear
#> Group 14: nanoseconds, microseconds, picoseconds, milliseconds
#> Group 15: period_to_seconds, check_period, multiply_period_by_number, format.Period, divide_period_by_number, add_period_to_period
#> Group 16: myd, dmy, yq, ymd, dym, mdy, ydm
#> Group 17: hours, weeks, minutes, years, days, months.numeric, seconds, seconds_to_period
#> Group 18: C_force_tz, hour.default, mday.default, c.POSIXct, .mklt, yday.default, year.default, minute.default, second.default
#> Group 19: ehours, emilliseconds, eyears, eseconds, epicoseconds, enanoseconds, eminutes, olson_time_zones, edays, emicroseconds, eweeks
#> Group 20: dmy_h, ydm_hms, ymd_hms, dmy_hm, ymd_h, ydm_hm, ydm_h, dmy_hms, ymd_hm, mdy_hms, mdy_hm, mdy_h
Now, we have identified twenty different groups of functions within lubridate that share the exact same dependencies. We could explore each group of functions for potential consolidation.
Edge direction was previously Independent -> Dependent. It was changed to Dependent -> Independent in version v0.3.0. The new convention follows the Unified Modeling Language (UML) framework, a widely used standard for software system modeling.↩︎
Examples from version 1.7.3 of Lubridate↩︎