Eisen
Eisen

Reputation: 1887

getting all dependencies of an R package when calling library

I have built my own R package, and I when I call library() on my package I want to also download all the dependencies while would appear in the DESCRIPTION file.

How does this work exactly? I can library on my package and I have to individually call library on some of the packages that are used in my package.

Upvotes: 1

Views: 751

Answers (1)

Dirk is no longer here
Dirk is no longer here

Reputation: 368181

You can do what R does for you by relying on some public information:

> db <- tools::CRAN_package_db()    # a big matrix with all packages
> deps <- tools::package_dependencies("ggplot2", recursive=TRUE, db=db)
> deps                   # a list as you could have called with more than arg
$ggplot2
 [1] "digest"       "glue"         "grDevices"    "grid"         "gtable"      
 [6] "isoband"      "MASS"         "mgcv"         "rlang"        "scales"      
[11] "stats"        "tibble"       "withr"        "utils"        "methods"     
[16] "graphics"     "nlme"         "Matrix"       "splines"      "farver"      
[21] "labeling"     "lifecycle"    "munsell"      "R6"           "RColorBrewer"
[26] "viridisLite"  "fansi"        "magrittr"     "pillar"       "pkgconfig"   
[31] "vctrs"        "lattice"      "colorspace"   "cli"          "utf8"        

> 

So by calling the package_dependencies (base R) function you get the list of all dependencies, either level one or fully recursively.

You could then check this with installed.packages() to see what if anything is missing in the local installation.

Upvotes: 4

Related Questions