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

时间:2017-08-25 17:35:52

标签: r tidyverse pkgdown

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

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

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

谢谢!

3 个答案:

答案 0 :(得分:2)

如果你不介意运行一个函数来更新你的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的“其他”部分(如果尚未存在则会自动创建)。

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

HTH!

答案 1 :(得分:2)

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]
}

答案 2 :(得分:0)

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

正则表达式否定效率不高,您必须重新键入已分类函数的名称,因此这可能会在您的有限情况下起作用,但这不是最佳做法。

这样的事情会起作用吗? (test here

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