简体   繁体   中英

List the packages imported by a given package precisely using R code?

To see which packages are imported by any given package, we could visit the manual, and view under 'imports', or we could view the DESCRIPTION file if the repository is on github, but how can we do this using R code?

For example, if such a function were called imports() , and it were called on the rvest package, then imports(rvest) would return something like

[1] httr (>= 0.5), magrittr, selectr

Note: a less elegant way of seeing which packages a package imports could be to start a fresh R session, view the loaded packages, then load the package in question, and compare the lists (if there are many more loaded packages, those were imported by the package in question) - but I prefer not to use this method as it would require frequently starting a new R session.

You should take a look at the packageDescription function from the utils R package: https://stat.ethz.ch/R-manual/R-devel/library/utils/html/packageDescription.html . it does exactly that, parse the DESCRIPTION file and returns a named list with the results.

For packages hosted on CRAN (or some specified R repository), this information is already handy in the matrix returned by available.packages() :

a1 <- available.packages()
a1["rvest","Imports"]
## [1] "httr (>= 0.5), magrittr, selectr"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM