我想得到一个名称与给定模式匹配的所有函数的列表。例如,我希望所有的功能都包含" theme _"。
我看过this post,它提供了一个获取名字矢量的解决方案。是否有可能与函数列表相同而不是名称向量?
答案 0 :(得分:4)
对于本地软件包,您可以尝试这样做:
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"