2017-08-25 22 views
4

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

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

但是这使myFunction同时在“someCategory”和“其他”类别。我想要做的是匹配所有不属于某个类别的功能。

谢谢!

回答

1

如果你不介意的运行功能来更新您的YAML,从你的包的根目录运行这应该工作(使用“覆盖= 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_rastsetClasses``setinfo_rastsprawl_scalebar到 _pkgdown.yaml

功能浏览当前.yaml文件,查找当前缺少的功能。如果发现任何内容,则将其添加到.yaml的“其他”部分(如果尚不存在,则会自动创建)。

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

HTH!

0

我不熟悉pkgdown,但对于这样一个有限的情况下,可以使用matches用正则表达式可行并不等于这些

正则表达式否定的效率不高,而且你会必须重新键入分类功能的名称,这样可能会在有限的情况下起作用,但不是最佳做法。

会这样的工作? (test here

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

对于有些tricker和使用效率较低的情况下,请参见[正则表达式串不含有** **多个特定的词(https://stackoverflow.com/questions/7801581/regex-for-string-不包含多个特定词) – C8H10N4O2

+0

这是一个好主意,但我认为它不适用于很多功能(比如说你在包中有一百个函数),而且大部分我试图去捕捉的是有人添加了一个新功能,但忘记更新'yml' – Shorpy

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] 
} 
相关问题