Finding functions using grep over multiple loaded packages in R

Suppose I have base , dplyr , data.table , tidyr , etc. packages downloaded using sapply() .

  sapply(c("dplyr","data.table","tidyr"),library,character.only=TRUE) 

So, to check the list of functions in a specific package, I do

  ls("package:data.table") 

Now, if I want to look for functions inside dplyr , starting with the is. template is. i do

  grep("is\\.",ls("package:dplyr"),value=TRUE) # [1] "is.grouped_df" "is.ident" "is.sql" "is.src" # [5] "is.tbl" 

My goal is to search for all functions starting with is. or as. , or any other template in several packages at the same time. A code that I think would be long, i.e. Below. I combined the list of dplyr and base functions and then added the grep template. How to do this for many downloaded packages?

  grep("is\\.",c(ls("package:dplyr"),ls("package:base")),value=T) 

The search() function will provide me with a list of downloaded packages. But how to collect all the functions of the downloaded packages so that I can grep later on it.

For one package, a list of functions can be obtained using

  ls("package:package_name") 

Any help is appreciated.

+7
regex grep r pattern-matching perl
source share
2 answers

For a list of downloadable packages, use:

 x <- grep('package:',search(),value=TRUE) # Comment below by danielson # eg ("package:base", "package:data.table") sapply(x, function(x) { paste0(x, ":", grep("is\\.", ls(x),value=TRUE)) }) 

Output:

 $`package:base` [1] "package:base:is.array" "package:base:is.atomic" [3] "package:base:is.call" "package:base:is.character" [5] "package:base:is.complex" "package:base:is.data.frame" [7] "package:base:is.double" "package:base:is.element" ... $`package:data.table` [1] "package:data.table:is.data.table" 
+6
source share

Thanks to everyone. So, here are the answers in 3 versions to the question that I posted with all the help here.

Note. The priority was to search for a template over several downloaded packages, you can search for another template instead of "is."

VERSION 1

  ## Get list of loaded packages into XX <- grep('package:',search(),value=TRUE) # Comment by danielson # Use grep to find functions with is. pattern over multiple loaded packages inside X sapply(X, function(X) { paste0(X, ":", grep("is\\.", ls(X),value=TRUE)) }) 

VERSION 2

  ## Get list of loaded packages into XX <- .packages() # Comment by docendo discimus # Use grep to find functions with is. pattern over multiple loaded packages inside X sapply(X, function(X) { paste0(X, ":", grep("is\\.", ls(paste0("package:",X)),value=TRUE)) }) 

VERSION 3 - Without grep () function

  ## Get list of loaded packages into XX <- .packages() # find functions using `ls()` with is. pattern over multiple loaded packages inside X # Suggested by Rich Scriven Over Comments sapply(X, function(X) { paste0(X,":",ls(paste0("package:",X), pattern = "is\\.")) }) 
0
source share

All Articles