Regex 有没有办法得到一个包含所有函数的列表,这些函数的名称与给定的正则表达式匹配?

Regex 有没有办法得到一个包含所有函数的列表,这些函数的名称与给定的正则表达式匹配?,regex,r,Regex,R,我想得到一个名称与给定模式匹配的所有函数的列表。例如,我希望所有函数的名称都包含“theme_u2;” 我已经看到了,它给出了一个得到名称向量的解决方案。有没有可能将其作为函数列表而不是名称向量 对于本地软件包,您可以尝试以下方法: if (!require("pacman")) install.packages("pacman"); library(pacman) regex <- "theme_" packs <- p_lib() out <- setNames(lap

我想得到一个名称与给定模式匹配的所有函数的列表。例如,我希望所有函数的名称都包含“theme_u2;”


我已经看到了,它给出了一个得到名称向量的解决方案。有没有可能将其作为函数列表而不是名称向量

对于本地软件包,您可以尝试以下方法:

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

返回名称向量,而不是函数列表。实际上,您可能需要
lappy(apropos(“theme_”),mode=“function”),get)
而不是
mget()
。但您的问题并不是要具体说明在哪里搜索这些函数。理想情况下,我希望在CRAN和Github上找到所有函数,但首先,我希望查看安装在我计算机上的所有库
## $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"