landau landau - 23 days ago 7
R Question

List the dependencies of a package regardless of how it is loaded or installed

I am looking for a slick way to programmatically get the

Imports:
and
Depends:
packages of a package. The trick is that the method should work regardless of whether the package is


  1. installed or just loaded with
    devtools::load_all()
    , or

  2. attached or just available through the namespace.



Desired functionality:

package_deps("dplyr")
## [1] "assertthat" "bindrcpp" "glue" "magrittr" "methods" "pkgconfig"
## [7] "rlang" "R6" "Rcpp" "tibble" "utils"


Notice that the version numbers are all stripped away.

I am writing this post because it seems like this should already exist. (After all,
install.packages()
needs similar functionality.) I would rather not have to manage all the special cases myself.

Answer Source

The pacman package that I maintain does this:

if (!require("pacman")) install.packages("pacman")
pacman::p_depends(dplyr)

$Imports
 [1] "assertthat" "bindrcpp"   "glue"       "magrittr"   "methods"   
 [6] "pkgconfig"  "rlang"      "R6"         "Rcpp"       "tibble"    
[11] "utils"     

$LinkingTo
[1] "Rcpp"     "BH"       "bindrcpp" "plogr"   

$Suggests
 [1] "bit64"          "covr"           "dbplyr"         "dtplyr"        
 [5] "DBI"            "ggplot2"        "hms"            "knitr"         
 [9] "Lahman"         "mgcv"           "microbenchmark" "nycflights13"  
[13] "rmarkdown"      "RMySQL"         "RPostgreSQL"    "RSQLite"       
[17] "testthat"       "withr"