【问题标题】:Is there a way to get a list with of all functions which names match a given regular expressions?有没有办法获取名称与给定正则表达式匹配的所有函数的列表?
【发布时间】:2015-10-22 14:33:41
【问题描述】:

我想获取名称与给定模式匹配的所有函数的列表。例如,我想拥有名称中包含“theme_”的所有函数。

我见过this post,它提供了获取名称向量的解决方案。是否可以与函数列表而不是名称向量相同?

【问题讨论】:

  • 返回名称向量,而不是函数列表
  • 实际上,您可能需要lapply(apropos("theme_", mode = "function"), get) 而不是mget()。但您的问题并未具体说明您要在哪里搜索这些功能。
  • 理想情况下,我想在 CRAN 和 Github 上找到所有功能,但首先,我想查看我计算机上安装的所有库

标签: regex r


【解决方案1】:

对于本地包,你可以试试这个:

if (!require("pacman")) install.packages("pacman"); library(pacman)

regex <- "theme_"
packs <- p_lib()

out <- setNames(lapply(packs, function(x){
    funs <- try(p_funs(x, character.only=TRUE))
    if (inherits(funs, "try-error")) return(character(0))
    funs[grepl(regex, funs)]
}), packs)

out[!sapply(out, identical, character(0))]

这是我的输出:

## $cowplot
## [1] "theme_cowplot" "theme_nothing"
## 
## $ggplot2
##  [1] "theme_blank"    "theme_bw"       "theme_classic"  "theme_get"      "theme_gray"     "theme_grey"     "theme_light"    "theme_line"     "theme_linedraw" "theme_minimal" 
## [11] "theme_rect"     "theme_segment"  "theme_set"      "theme_text"     "theme_update"  
## 
## $gridExtra
## [1] "ttheme_default" "ttheme_minimal"
## 
## $plotflow
## [1] "theme_apa"   "theme_basic" "theme_black" "theme_map"  
## 
## $qdap
## [1] "theme_badkitchen" "theme_cafe"       "theme_duskheat"   "theme_grayscale"  "theme_greyscale"  "theme_hipster"    "theme_nightheat"  "theme_norah"     

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-11
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 2017-08-16
    • 2013-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多