When developing R packages, we should try to avoid directly setting dependencies on “heavy packages”. The “heaviness” for a package means, the number of additional dependency packages it brings to. If your package directly depends on a heavy package, it would bring several consequences:
sessionInfo()
).In the DESCRIPTION file of your package, there are “direct dependency pakcages” listed in the Depends
, Imports
and LinkingTo
fields. There are also “indirect dependency packages” that can be found recursively for each of the direct dependency packages. Here what we called “dependency packages” are the union of the direct and indirect dependency packages.
There are also packages listed in Suggests
and Enhances
fields in DESCRIPTION file, but they are not enforced to be installed when installing your package. Of course, they also have “indirect dependency packages”. To get rid of the heavy packages that are not often used in your package, it is better to move them into the Suggests
/Enhances
fields and to load/install them only when they are needed.
Here the pkgndep package checks the heaviness of the dependency packages of your package. For each package listed in the Depends
, Imports
, LinkingTo
and Suggests
/Enhances
fields in the DESCRIPTION file, pkgndep checks how many additional packages your package requires. The summary of the dependency is visualized by a customized heatmap.
As an example, I am developing a package called cola which depends on a lot of other packages. The dependency heatmap looks like follows (please drag the figure to a new tab to see it in its actual size):
In the heatmap, rows are the packages listed in Depends
, Imports
and Suggests
fields, columns are the additional dependency packages required for each row package. The barplots on the right show the number of required package, the number of imported functions/methods/classes (parsed from NAMESPACE file) and the quantitative measure “heaviness” (the definition of heaviness will be introduced later).
We can see if all the packages are put in the Depends
or Imports
field (i.e. movig all suggsted packages to Imports
), in total 257 packages are required, which are really a lot. Actually some of the heavy packages such as WGCNA, clusterProfiler and ReactomePA (the last three packages in the heatmap rows) are not very frequently used in cola, moving them to Suggests
field and using them only when they are needed greatly helps to reduce the dependencies of cola. Now the number of required packages are reduced to only 65.
To use this package:
library(pkgndep)
pkg = pkgndep("package-name") # if the package is already installed
dependency_heatmap(pkg)
or
pkg = pkgndep("path-of-the-package") # if the package has not been installed yet
dependency_heatmap(pkg)
The value for pkgndep()
should be 1. a CRAN/Bioconductor package, 2. an installed package, 3. a path of a local package, 4. URL of a GitHub repository.
Executable examples:
## retrieve package database from CRAN/Bioconductor (3.16)...
## - 21717 remote packages on CRAN/Bioconductor.
## - 761 packages installed locally.
## prepare dependency table...
## prepare reverse dependency table...
pkg
## 'ComplexHeatmap', version 2.15.1
## - 30 packages are required for installing 'ComplexHeatmap'.
## - 125 packages are required if installing packages listed in all fields in DESCRIPTION.
pkgndep()
first needs to retrieve package databases both from remote repositories and local libraries, as you can see the message from above code. This only happens once and the database is internally saved and re-used.
We can directly use dependency_heatmap()
function to create the dependency heatmap:
dependency_heatmap(pkg)
You can set the file
argument to directly save the image into a figure where the figure size is automatically calculated. Supported image formats are png
/jpg
/svg
/pdf
.
dependency_heatmap(pkg, file = "test.png")
heaviness_report()
function can generate an HTML report for the dependency heaviness analysis on the package.
heaviness_report(pkg)
The heaviness of package dependency can be measured quantitatively. pkgndep provides two measures: the absolute measure and the relative measure.
The heaviness of a dependency package is calculated as follows. If package B is in the Depends
/Imports
/LinkingTo
fields of package A, which means, package B is directly required for package A, denote v1
as the total number of packages for package A, and denote v2
as the total number of required packages if moving package B to Suggests
in package A (which means, now B is not enforced to be installed for package A). The absolute measure of heaviness is simply v1 - v2
and relative measure is (v1 + a)/(v2 + a)
where a
is a small constant, e.g. 10. So here the absolute heaviness for package B on package A is the number of additional packages that package B uniquely brings in.
In the second scenario, if package B is in the Suggests
/Enhances
fields of package A, now v2
is the total number of required packages if moving package B to Imports
in package A, the absolute measure of heaviness is v2 - v1
and relative measure is (v2 + a)/(v1 + a)
.
The heaviness score can be calculated by the function heaviness()
:
heaviness(pkg)
## grDevices graphics grid stats methods
## 0 0 0 0 0
## RColorBrewer png matrixStats codetools digest
## 1 1 1 0 1
## foreach colorspace GlobalOptions clue doParallel
## 0 0 0 2 4
## GetoptLong IRanges circlize dendsort jpeg
## 3 4 2 1 1
## tiff fastcluster Cairo gridGraphics glue
## 1 1 1 1 1
## grImport markdown gplots grImport2 GenomicRanges
## 2 4 5 4 7
## knitr pheatmap gridtext rmarkdown testthat
## 13 12 17 29 29
## dendextend EnrichedHeatmap
## 31 12
heaviness(pkg, rel = TRUE)
## grDevices graphics grid stats methods
## 1.000000 1.000000 1.000000 1.000000 1.000000
## RColorBrewer png matrixStats codetools digest
## 1.025641 1.025641 1.025641 1.000000 1.025641
## foreach colorspace GlobalOptions clue doParallel
## 1.000000 1.000000 1.000000 1.052632 1.111111
## GetoptLong IRanges circlize dendsort jpeg
## 1.081081 1.111111 1.052632 1.025000 1.025000
## tiff fastcluster Cairo gridGraphics glue
## 1.025000 1.025000 1.025000 1.025000 1.025000
## grImport markdown gplots grImport2 GenomicRanges
## 1.050000 1.100000 1.125000 1.100000 1.175000
## knitr pheatmap gridtext rmarkdown testthat
## 1.325000 1.300000 1.425000 1.725000 1.725000
## dendextend EnrichedHeatmap
## 1.775000 1.300000
tools::package_dependencies()
The package dependencies are based on “package database” which is normally retrieved by available.packages()
. In tools package, there is a package_dependencies()
function that can be used to get a list of dependency packages. In the following example code, we retrieve the dependency packages for package ggplot2.
chooseCRANmirror(ind = 1) # choose the mirror fro RStudio
db = available.packages()
system.time(p1 <- tools::package_dependencies("ggplot2", db = db, recursive = TRUE)[[1]])
## user system elapsed
## 0.205 0.001 0.206
In pkgndep, we implement a faster version of package_dependencies()
function. First the database needs to be reformatted by reformat_db()
function. The returned variable db2
is a reference class object and its method db2$package_dependencies()
can be used to retrieve dependency packages.
db2 = reformat_db(db)
## prepare dependency table...
## prepare reverse dependency table...
db2
## A package database of 18961 packages.
## - 18961 CRAN / 0 Bioconductor / 0 other packages.
system.time(p2 <- db2$package_dependencies("ggplot2", recursive = TRUE, simplify = TRUE))
## user system elapsed
## 0.003 0.000 0.004
p1
and p2
are actually identical:
## [1] TRUE
## R version 4.2.0 (2022-04-22)
## Platform: x86_64-apple-darwin17.0 (64-bit)
## Running under: macOS Big Sur/Monterey 10.16
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib
##
## locale:
## [1] C/UTF-8/C/C/C/C
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] pkgndep_1.99.0
##
## loaded via a namespace (and not attached):
## [1] circlize_0.4.16 shape_1.4.6 GetoptLong_1.1.0
## [4] xfun_0.35 bslib_0.4.2 BiocVersion_3.16.0
## [7] purrr_0.3.5 colorspace_2.0-3 vctrs_0.5.1
## [10] htmltools_0.5.4 stats4_4.2.0 hash_2.2.6.2
## [13] yaml_2.3.6 rlang_1.0.6 pkgdown_2.0.7
## [16] jquerylib_0.1.4 glue_1.6.2 BiocGenerics_0.44.0
## [19] RColorBrewer_1.1-3 matrixStats_0.63.0 foreach_1.5.2
## [22] lifecycle_1.0.3 stringr_1.5.0 ragg_1.2.4
## [25] GlobalOptions_0.1.2 codetools_0.2-18 memoise_2.0.1
## [28] evaluate_0.19 knitr_1.41 ComplexHeatmap_2.15.1
## [31] IRanges_2.32.0 fastmap_1.1.0 Cairo_1.6-0
## [34] doParallel_1.0.17 parallel_4.2.0 highr_0.9
## [37] Rcpp_1.0.9 cachem_1.0.6 desc_1.4.2
## [40] S4Vectors_0.36.1 magick_2.7.3 jsonlite_1.8.4
## [43] systemfonts_1.0.4 fs_1.5.2 textshaping_0.3.6
## [46] rjson_0.2.21 png_0.1-8 digest_0.6.31
## [49] stringi_1.7.8 grid_4.2.0 rprojroot_2.0.3
## [52] clue_0.3-63 cli_3.4.1 tools_4.2.0
## [55] magrittr_2.0.3 sass_0.4.4 cluster_2.1.4
## [58] crayon_1.5.2 rmarkdown_2.19 iterators_1.0.14
## [61] R6_2.5.1 compiler_4.2.0