简体   繁体   English

使用 R 代码精确列出给定包导入的包?

[英]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?要查看任何给定包导入了哪些包,我们可以访问手册并在“导入”下查看,或者如果存储库在 github 上,我们可以查看DESCRIPTION文件,但我们如何使用 R 代码执行此操作?

For example, if such a function were called imports() , and it were called on the rvest package, then imports(rvest) would return something like例如,如果这样的函数被称为imports() ,并且它是在rvest包上调用的,那么imports(rvest)将返回类似

[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.注意:查看包导入哪些包的不太优雅的方法是启动一个新的 R 会话,查看加载的包,然后加载有问题的包,并比较列表(如果有更多加载的包,那些是导入的由有问题的包) - 但我不想使用这种方法,因为它需要经常启动一个新的 R 会话。

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 .您应该查看utils R 包中的packageDescription函数: 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.它正是这样做的,解析DESCRIPTION文件并返回一个包含结果的命名列表。

For packages hosted on CRAN (or some specified R repository), this information is already handy in the matrix returned by available.packages() :对于托管在 CRAN(或某些指定的 R 存储库)上的包,此信息在available.packages()返回的矩阵中已经很方便了:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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