gpt4 book ai didi

r - 确定所有包依赖项的最低 R 版本

转载 作者:行者123 更新时间:2023-12-04 10:08:22 26 4
gpt4 key购买 nike

我是一名软件包开发人员,想在 DESCRIPTION 中说明使用我的软件包所需的最低 R 版本。文件。
available.packages解析包的描述,但结果不是(容易)机器可读以查找递归依赖项,因为导入和依赖字段是逗号分隔的文本,有时包含具有版本要求的包。

中描述的解决方案:
Listing R Package Dependencies Without Installing Packages不是递归解决方案。如果嵌套依赖需要 R > 3.3,我想知道它。

至少,我希望看到 R 的最低版本以及给定 CRAN 包的导入、链接和依赖包。更好的是列出设置最低 R 或包版本的包。

通过消除具有更高版本要求的依赖项,我可以为更多人提供他们无法修复的机构旧 R 版本:有些人仍在 R 2.x 上。

最佳答案

min_r_version <- function(package="ggplot2", exclude_main_pkg=TRUE) {

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

deps <- package_dependencies(package, recursive=TRUE)

if (exclude_main_pkg) {
pkgs <- deps[[1]]
} else {
pkgs <- c(package, deps[[1]])
}

available.packages() %>%
as_data_frame() %>%
filter(Package %in% pkgs) %>%
select(Depends) %>%
unlist() -> pkg_list

# if main pkg only relied on core R packages (i.e. pkgs that aren't in CRAN) and we
# excluded the pkg itself from the min version calculation, this is an edge case we need
# to handle.

if (length(pkg_list) == 0) return("Unspecified")

stri_split_regex(pkg_list, "[,]") %>%
unlist() %>%
trimws() %>%
stri_match_all_regex(c("^R$|^R \\(.*\\)$")) %>%
unlist() %>%
discard(is.na(.)) %>%
unique() %>%
stri_replace_all_regex("[R >=\\(\\)]", "") %>%
data_frame(vs=.) %>%
separate(vs, c("a", "b", "c"), fill="right") %>%
mutate(c=ifelse(is.na(c), 0, c)) %>%
arrange(a, b, c) %>%
tail(1) %>%
unite(min, a:c, sep=".") -> vs

return(vs$min)

}

# did we handle the edge cases well enought?
base <- c("base", "compiler", "datasets", "grDevices", "graphics", "grid", "methods", "parallel", "profile", "splines", "stats", "stats4", "tcltk", "tools", "translations")
(base_reqs <- purrr::map_chr(base, min_r_version))
## [1] "Unspecified" "Unspecified" "Unspecified" "Unspecified" "Unspecified"
## [6] "Unspecified" "Unspecified" "Unspecified" "Unspecified" "Unspecified"
## [11] "Unspecified" "Unspecified" "Unspecified" "Unspecified" "Unspecified"

# a few of the "core" contributed pkgs rely on a pkg or two outside of base
# but many only rely on base packages, to this is another gd edge case to
# text for.
contrib <- c("KernSmooth", "MASS", "Matrix", "boot", "class", "cluster", "codetools", "foreign", "lattice", "mgcv", "nlme", "nnet", "rpart", "spatial", "survival")
contrib_reqs <- purrr::map_chr(contrib, min_r_version)
## [1] "Unspecified" "Unspecified" "3.0.0" "Unspecified" "3.1.0"
## [6] "Unspecified" "Unspecified" "Unspecified" "Unspecified" "3.0.2"
## [11] "3.0.0" "Unspecified" "Unspecified" "Unspecified" "3.0.1"

# See what the min version of R shld be for some of my pkgs
min_r_version("ggalt") # I claim R (>= 3.0.0) in DESCRIPTION
## [1] "3.1.2"

min_r_version("curlconverter") # I claim R (>= 3.0.0) in DESCRIPTION
## [1] "3.1.2"

min_r_version("iptools") # I claim R (>= 3.0.0) in DESCRIPTION
## [1] "3.0.0"

关于r - 确定所有包依赖项的最低 R 版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38686427/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com