本文介绍了如何在DESCRIPTION中自动添加/更新Depends / Imports / Suggests版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

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

I like to keep my R packages up to date, and in developing my own package, I want to stick to @Hadley's advice:

所以,我会在说明中需要其中一些:

So, I'll need some of those here in DESCRIPTION:

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

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

Is there an existing tool to programmatically update the versions of dependencies based on the packages I currently have installed?

我知道这是一件小事,我可以手动执行此操作,但我只知道这是很容易忘记的事情。

I know this is a small thing and I can do this manually, but I just know this is the kind of thing that is easily forgotten.

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

Using the locally installed version of a package as a minimum dependency also seems to make sense because I tested / build with those dependencies.

我是否有理由不应该这样做?

Is there a reason why I shouldn't do this?

推荐答案

这将按照您的要求进行(嗯,您仍然需要将输出剪切/粘贴到 DESCRIPTI中ON :-)

This shld do what you ask (well, you still need to cut/paste the output into 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)

仅显示要处理的极端情况:

Just to show edge cases are handled:

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)

这篇关于如何在DESCRIPTION中自动添加/更新Depends / Imports / Suggests版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 22:15