如何从DESCRIPTION文件快速/轻松地安装所有需要的软件包?

gcmastyq  于 2023-11-14  发布在  其他
关注(0)|答案(2)|浏览(116)

R包,repos,其他项目(例如bookdown)通常有一个名为DESCRIPTION的文件,其中列出了项目所需的包。
有没有一种快速简单的方法来安装所需的软件包?

kcugc4gi

kcugc4gi1#

如果你想要最小的依赖,那么你可以写一个基于read.dcf的 Package 器。我的 Package 器接受一个DESCRIPTION文件,并返回一个字符向量,列出所描述的包的依赖(可选反向,可选递归)。你可以通过将结果或子集传递给install.packages来安装(像往常一样)。

function(file, db = NULL, ...) {
    dsc <- read.dcf(file)[1L, ] # assuming ASCII; see tools:::.read_description
    pkg <- dsc["Package"]
    if (is.null(db))
        db <- utils::available.packages()
    db <- rbind(db[db[, "Package"] != pkg, , drop = FALSE], 
                dsc[colnames(db)])
    tools::package_dependencies(pkg, db, ...)[[1L]]
}

字符串
我刚刚测试了一下...它似乎做了我想要的...

> description_dependencies <- function(file, db = NULL, ...) {
+     dsc <- read.dcf(file)[1L, ] # assuming ASCII; see tools:::.read_description
+     pkg <- dsc["Package"]
+     if (is.null(db))
+         db <- utils::available.packages()
+     db <- rbind(db[db[, "Package"] != pkg, , drop = FALSE],
+                 dsc[colnames(db)])
+     tools::package_dependencies(pkg, db, ...)[[1L]]
+ }
> tf <- tempfile()
> writeLines(con = tf,
+            c("Package: zzz",
+              "Version: 0.0.1",
+              "Depends: R (>= 3.5.0), methods",
+              "LinkingTo: Matrix (>= 1.6-1)",
+              "Imports:\n\tstats,\n\ttools,\n\tutils",
+              "Suggests: lattice, nlme",
+              "Enhances: MatrixModels"))
> db <- available.packages() # expensive, so reuse
> description_dependencies(tf, db, which = "all")
[1] "methods"      "stats"        "tools"        "utils"        "Matrix"      
[6] "lattice"      "nlme"         "MatrixModels"
> description_dependencies(tf, db, which = "most")
[1] "methods" "stats"   "tools"   "utils"   "Matrix"  "lattice" "nlme"   
> description_dependencies(tf, db, which = "strong")
[1] "methods" "stats"   "tools"   "utils"   "Matrix" 
> description_dependencies(tf, db, which = c("Suggests", "Enhances"))
[1] "lattice"      "nlme"         "MatrixModels"

kq0g1dla

kq0g1dla2#

以下是如何安装DESCRIPTION文件中列出的所有软件包:

remotes::install_deps(upgrade = "always")

字符串
upgrade = "always"参数自动响应1(是的,安装所有),以询问是否也更新这些软件包的依赖关系。

相关问题