9

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

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

但这myFunction同时包含“someCategory”和“other”类别。我想做的是匹配所有不在一个类别中的函数。

谢谢!

4

3 回答 3

5

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]
}
于 2017-09-02T11:23:16.767 回答
3

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

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

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

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

于 2017-09-02T10:20:26.320 回答
0

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

正则表达式否定效率不高,您将不得不重新输入分类函数的名称,因此这可能在您的有限情况下仍然有效,但不是最佳实践。

像这样的东西会起作用吗?(这里测试

- title: "other"
  contents:
  - matches('^(?!.*(myFunction|myOtherFunction|yetAnotherFunction)).*$')
于 2017-08-29T03:15:30.563 回答