正如Ben在 comments 中指出的,您需要获得fields
的依赖项,然后过滤掉优先级为"Base"
或"Recommended"
的包,然后将包列表传递给install.packages()
以处理安装.比如:
instPkgPlusDeps <- function(pkg, install = FALSE,
which = c("Depends", "Imports", "LinkingTo"),
inc.pkg = TRUE) {
stopifnot(require("tools")) ## load tools
ap <- available.packages() ## takes a minute on first use
## get dependencies for pkg recursively through all dependencies
deps <- package_dependencies(pkg, db = ap, which = which, recursive = TRUE)
## the next line can generate warnings; I think these are harmless
## returns the Priority field. `NA` indicates not Base or Recommended
pri <- sapply(deps[[1]], packageDescription, fields = "Priority")
## filter out Base & Recommended pkgs - we want the `NA` entries
deps <- deps[[1]][is.na(pri)]
## install pkg too?
if (inc.pkg) {
deps = c(pkg, deps)
}
## are we installing?
if (install) {
install.packages(deps)
}
deps ## return dependencies
}
这使得:
R> instPkgPlusDeps("fields")
Loading required package: tools
[1] "fields" "spam" "maps"
哪个和
> packageDescription("fields", fields = "Depends")
[1] "R (>= 2.13), methods, spam, maps"
您会从sapply()
行if收到警告,deps
中的依赖项实际上没有安装.我认为这些是无害的,因为在这种情况下返回的值是NA
,我们用它来表示我们想要安装的软件包.如果你安装了4000个软件包,我怀疑这会影响你.
默认设置是not,用于安装软件包,但只返回依赖项列表.我认为这是最安全的,因为你可能没有意识到其中隐含的依赖链,最终意外地安装了数百个软件包.如果您愿意安装所示的软件包,请输入install = TRUE
.
请注意,我限制了搜索到的依赖项的类型——如果使用which = "most"
,那么在递归解析这些依赖项(也包括Suggests:
个字段)后,fields有300多个这样的依赖项.which = "all"
将寻找所有东西,包括Enhances:
,这将是一个更大的包裹 list .关于which
参数的有效输入,请参见?tools::package_dependencies
.