gpt4 book ai didi

r - 在 pkgdown 引用 yaml 中包含 "All other functions"

转载 作者:行者123 更新时间:2023-12-04 15:18:27 31 4
gpt4 key购买 nike

我有一个 pkgdown在该站点中,我将引用 .yml 中的许多功能按类别分组文件。我想知道是否有一种方法可以将所有我没有明确分类的功能归入它们自己的类别。我唯一的想法是使用 matches功能如下:

reference:
- title: "someCategory"
contents:
- myFunction
- title: "other"
contents:
- matches(".*")

但是这把 myFunction在“someCategory”和“other”类别中。我想做的是匹配所有不在类别中的功能。

谢谢!

最佳答案

如果您不介意运行一个函数来更新您的 yaml,那么从包的根目录运行它应该可以工作(使用“overwrite = FALSE”进行测试:它将创建一个 _pkgdown_new.yaml 文件。):

update_yaml <- function(mypkg, overwrite = FALSE) {
require(yaml)
# _____________________________________________________________________
# Find currently missing functions in yaml file ####
curr_yaml <- yaml.load_file("_pkgdown.yaml")
curr_yaml_ref <- curr_yaml[["reference"]]
curr_funcs <- unlist(lapply(curr_yaml_ref,
FUN = function(x) (x$contents))) %>%
gsub('`', "", .)
all_pkgfuncs <- ls(paste0("package:", mypkg))
miss_funcs <- setdiff(pkg_funcs, curr_funcs)

if (length(miss_funcs) == 0) {
message("All functions are already in _pkgdown.yaml")
} else {

# _________________________________________________________________
# Look if an "Other" section already exists ####

titles <- unlist(lapply(curr_yaml_ref, FUN = function(x) (x$title)))
other_sect <- which(titles == "Other")

if (!length(other_sect) == 0) {
# _________________________________________________________________
# If the "Other" sect already exists, append missing functions ####

message(strwrap(paste(
"Adding ", paste0("`", miss_funcs, "` ", collapse = ""),
"to _pkgdown.yaml")))
curr_yaml_ref[[other_sect]] = list(
title = "Other",
desc = "Other Functions",
contents = c(curr_yaml_ref[[other_sect]]$contents,
paste0("`", miss_funcs, "`"))
)

} else {

# _____________________________________________________________
# Otherwise, create the "other" section and add ####

message("Creating the \"Others\" section")
message(strwrap(paste(
"Adding ", paste0("`", miss_funcs, "` ", collapse = ""),
"to _pkgdown.yaml")))
curr_yaml_ref[[length(curr_yaml_ref) + 1]] = list(
title = "Other",
desc = "Other Functions",
contents = paste0("`", miss_funcs, "`"))
}
curr_yaml[["reference"]] <- curr_yaml_ref
if (overwrite) {
write(as.yaml(curr_yaml), "_pkgdown.yaml")
} else {
write(as.yaml(curr_yaml), "_pkgdown_new.yaml")
}
}
}

> update_yaml("sprawl", overwrite = F)

Creating the "Others" section
Adding er_crop_object er_getbands er_points er_polygons reproj_rast setClasses``setinfo_rast sprawl_scalebar to _pkgdown.yaml



该函数浏览当前 .yaml 文件并找到当前缺少的函数。如果找到,它们将被添加到 .yaml 的“其他”部分(如果不存在,则会自动创建)。

我做了一个快速测试,它似乎工作正常。

哈!

关于r - 在 pkgdown 引用 yaml 中包含 "All other functions",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45886789/

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