如何在 DESCRIPTION 中自动 add/update Depends/Imports/Suggests 版本?

How can I automatically add/update Depends/Imports/Suggests versions in DESCRIPTION?

我喜欢让我的 R 包保持最新,在开发我自己的包时,我想坚持使用@Hadley 的 advice:

Generally, it’s always better to specify the version and to be conservative about which version to require. Unless you know otherwise, always require a version greater than or equal to the version you’re currently using.

所以,我需要一些 DESCRIPTION:

Imports:
 knitr (>= 1.13),
 rmarkdown (>= 1.0)

是否有现成的工具可以根据我当前安装的包以编程方式更新依赖项的版本?

我知道这是一件小事,我可以手动完成,但我只知道这是一种很容易忘记的事情。

使用本地安装版本的包作为最小依赖项似乎也很有意义,因为我使用这些依赖项进行了测试/构建。

为什么我不应该这样做?

这将按照您的要求进行操作(好吧,您仍然需要 cut/paste 输出到 DESCRIPTION :-)

#' Add curent version string to package dependencies
#'
#' Will \code{cat} out a cut/paste-able set of fields for a
#' \code{DESCRIPTION} file with minimum required versions for
#' each package based upon currently available package vesions
#' in CRAN.
#'
#' @param pkg package description, can be path or package name
#' @param fields fields to get & report dependencies for
#' @note R and the R version is NOT added to \code{Depends}
#' @examples
#' add_pkg_versions("qmethod")
#' add_pkg_versions("MASS")
#' \dontrun { # assumes you're in a pkg devel dir
#' add_pkg_versions()
#' }
add_pkg_versions <- function(pkg=".",
                             fields=c("Depends", "Imports", "LinkingTo", "Suggests")) {

  require(purrr)
  walk(c("dplyr", "tools", "stringi", "devtools"), require, character.only=TRUE)

  stopifnot(is_scalar_character(pkg), pkg != "")
  fields <- match.arg(fields, c("Depends", "Imports", "LinkingTo", "Suggests"),
                      several.ok=TRUE)

  avail <- as_data_frame(available.packages())

  if (pkg == ".") {
    pkg_deps <- unclass(as_data_frame(read.dcf(file.path(package_file(), "DESCRIPTION"))))
    pkg <- pkg_deps$Package
    map(fields, ~stri_split_lines(pkg_deps[[.]])) %>%
      map(function(x) {
        if (length(x) > 0) {
          unlist(x) %>%
            stri_replace_all_regex(" \(.*$|,", "") %>%
            discard(`%in%`, c("", "R"))
        } else { x }
      }) -> pkg_deps
    names(pkg_deps) <- fields
  } else {
    pkg_deps <- map(fields, ~flatten_chr((package_dependencies(pkg,  which=.))))
    names(pkg_deps) <- fields
  }

  pkg_deps <- discard(pkg_deps, function(x) {length(x)==0})

  map(pkg_deps, function(x) {

    non_base <- filter(avail, Package %in% x)
    base <- setdiff(x, non_base$Package)

    non_base %>%
      mutate(pv=sprintf("%s (>= %s)", Package, Version)) %>%
      select(pv) %>%
      flatten_chr() -> pkg_plus_version

    sort(c(pkg_plus_version, base))

  }) -> pkg_deps

  cat("Package: ", pkg, "\n", sep="")
  walk(names(pkg_deps), function(x) {

    cat(x, ":\n", sep="")
    sprintf("    %s", pkg_deps[[x]]) %>%
      paste0(collapse=",\n") %>%
      cat()
    cat("\n")

  })

}

您的一个包裹:

add_pkg_versions("qmethod")

Package: qmethod
Imports:
    digest (>= 0.6.10),
    GPArotation (>= 2014.11-1),
    knitr (>= 1.13),
    methods,
    psych (>= 1.6.6),
    tools,
    xtable (>= 1.8-2)

只是为了说明处理了边缘情况:

add_pkg_versions("MASS")

Package: MASS
Depends:
    graphics,
    grDevices,
    stats,
    utils
Imports:
    methods
Suggests:
    lattice (>= 0.20-33),
    nlme (>= 3.1-128),
    nnet (>= 7.3-12),
    survival (>= 2.39-5)

另一个选项是 usethis 包,尽管这实际上会立即覆盖描述文件,所以不确定是否总是需要这样

# use overwrite = TRUE to update dependencies with version numbers
usethis::use_latest_dependencies(overwrite = TRUE, source = "CRAN")