【问题标题】:Include "All other functions" in a pkgdown reference yaml在 pkgdown 参考 yaml 中包含“所有其他功能”
【发布时间】:2018-02-03 19:43:52
【问题描述】:

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

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

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

谢谢!

【问题讨论】:

    标签: r tidyverse pkgdown


    【解决方案1】:

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

    【讨论】:

    • 这很有帮助。我运行mymiss&lt;-data_reference_index_missing(); paste(mymmiss, collapse = "- "),然后将其输出复制粘贴到.yml 中,添加返回。我喜欢下面的程序化方法,但无法正常工作。
    【解决方案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_objecter_getbandser_pointser_polygons reproj_rast setClasses``setinfo_rast sprawl_scalebar 到 _pkgdown.yaml

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

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

    HTH!

    【讨论】:

    • 这看起来应该非常有用,但我玩了几秒钟并没有让它工作。第一个问题是“.yaml”与“.yml”。 @F 的组合。女贞的方法,如果有这个方法真的很棒!
    【解决方案3】:

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

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

    这样的东西会起作用吗? (test here)

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

    【讨论】:

    • 对于一个有点诡计和效率较低的用例,请参阅regex for string not containing multiple specific words
    • 这是个好主意,但我认为这对于很多功能来说是不可行的(比如你在包中有一百个功能。我想要捕捉的主要是当有人添加一个新功能但忘记更新yml
    猜你喜欢
    • 2017-05-19
    • 2015-09-04
    • 2017-11-15
    • 1970-01-01
    • 1970-01-01
    • 2016-02-08
    • 1970-01-01
    • 2012-06-16
    • 1970-01-01
    相关资源
    最近更新 更多