0

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.

Eisen
  • 1,697
  • 9
  • 27
  • see the resolved part of this question https://stackoverflow.com/questions/37568884/r-package-does-not-load-dependencies?noredirect=1&lq=1 – shafee Sep 20 '22 at 16:16
  • 1
    I'm confused: you mention both *"download all dependencies"* (which should be handled with `install.packages("yourpackage")`) and then *"call library"*. In general, a package should never fully load another package into the user's environment; by including the dependent packages in the `NAMESPACE` as `import(data.table)` (e.g.) ensures that all of the functions in your package have full access to that package's functions. – r2evans Sep 20 '22 at 16:16
  • If you insist on side-effect when loading your package (rarely justified imo, so far the only popular exception is `library(tidyverse)`, which despite being popular/convenient is still a problem in some areas), then see the tidyverse package for how it does it. – r2evans Sep 20 '22 at 16:18
  • And the word **download** seems confusing in the xontext of your question. May be you mean that you wanted to load the dependency packages in your R session ?? – shafee Sep 20 '22 at 16:19
  • Other packages are loaded based on the Description file. Any packages in the **Depends** section of the description file will be automatically loaded when your package is loaded. Most package developers prefer to use **Imports** in the description file, which makes the other packages available internally, but does not presume to load them for the user. – Gregor Thomas Sep 20 '22 at 16:19
  • If I put dplyr in the imports section. Will calling library on my package load dplyr? – Eisen Sep 20 '22 at 16:51
  • https://stackoverflow.com/a/40391302/190277 – Ben Bolker Sep 20 '22 at 16:54

1 Answers1

2

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.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725