PAC
PAC

Reputation: 5376

Is there a way to get a list with of all functions which names match a given regular expressions?

I would like to get a list of all functions which name match a given pattern. For instance, I would like to have all functions which name includes "theme_".

I've seen this post which gives a solution to get a vector of names. Is it possible to have the same as a list of functions instead of a vector of names ?

Upvotes: 3

Views: 72

Answers (1)

Tyler Rinker
Tyler Rinker

Reputation: 109994

For local packages you might try this:

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

Here's my output:

## $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"     

Upvotes: 4

Related Questions