在 pkgdown 参考 yaml 中包含 "All other functions"

Include "All other functions" in a pkgdown reference yaml

我有一个 pkgdown 站点,在该站点中,我将许多功能分组到参考 .yml 文件中的类别中。我想知道是否有办法将我没有明确分类的所有功能都归入它们自己的类别。我唯一的想法就是像这样使用 matches 函数:

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

但这会将 myFunction 置于 "someCategory" 和 "other" 类别中。我想做的是匹配所有不在一个类别中的函数。

谢谢!

我不熟悉 pkgdown,但对于像这样的有限情况,将 matches 与正则表达式一起使用可能是可行的,因为 不等于其中任何一个

正则表达式的否定效率不高,您将不得不重新输入分类函数的名称,因此这同样适用于您的有限情况,但不是最佳做法。

这样的东西行得通吗? (test here)

- title: "other"
  contents:
  - matches('^(?!.*(myFunction|myOtherFunction|yetAnotherFunction)).*$')

如果你不介意 运行 更新你的 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 的 "Others" 部分(如果不存在则自动创建)。

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

嗨!

pkgdown 中,已经有一个功能可以警告您 yaml 文件中缺少的主题。 您可以通过键入 pkgdown:::data_reference_index.

查看代码

所以,基本上,如果您稍微修改这段代码,就可以 return 索引中缺少的函数的名称。

library(purrr)
data_reference_index_missing <- function(pkg = ".", depth = 1L) {
  pkg <- pkgdown:::as_pkgdown(pkg)

  meta <- pkg$meta[["reference"]] %||% default_reference_index(pkg)
  if (length(meta) == 0) {
    return(list())
  }

  # Cross-reference complete list of topics vs. topics found in index page
  all_topics <- meta %>%
    map(~ pkgdown:::select_topics(.$contents, pkg$topics)) %>%
    reduce(union)
  in_index <- seq_along(pkg$topics$name) %in% all_topics

  missing <- !in_index & !pkg$topics$internal
  pkg$topics$name[missing]
}