在pkgdown引用yaml中包含"所有其他函数"

Sho*_*rpy 6 r tidyverse pkgdown

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

reference:
- title: "someCategory"
  contents:
  - myFunction
- title: "other"
  contents:
  - matches(".*")
Run Code Online (Sandbox Code Playgroud)

但是,它myFunction同时包含"someCategory"和"other"类别.我想做的是匹配所有不在类别中的功能.

谢谢!

F. *_*ivé 6

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

因此,基本上,如果您只需稍微修改此代码,就可以返回索引中缺少的函数的名称。

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]
}
Run Code Online (Sandbox Code Playgroud)


lbu*_*ett 4

如果您不介意运行一个函数来更新您的 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)
Run Code Online (Sandbox Code Playgroud)

创建“Others”部分
添加er_crop_object er_getbands er_points er_polygons reproj_rast setClasses``setinfo_rast sprawl_scalebar到 _pkgdown.yaml

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

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

哈!