7

Lets say I have package base, dplyr, data.table, tidyr etc. loaded using sapply().

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

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

 ls("package:data.table")

Now if I want to search for functions inside dplyr starting with is. pattern 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 the functions starting with is. or as. or any other pattern in multiple packages simultaneously. The code I think would be lengthy i.e. below I have combined list of dplyr and base functions and then added grep pattern. How to do it for many loaded packages?

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

Function search() would give me list of loaded packages. But how to gather all the functions of loaded packages, so that I can later grep on it.

For a single package, list of functions can be obtained by

 ls("package:package_name")  

Any help is highly appreciated.

oguz ismail
  • 1
  • 16
  • 47
  • 69
Sowmya S. Manian
  • 3,723
  • 3
  • 18
  • 30

2 Answers2

7

To get a list of all packages which are loaded, use:

x <- grep('package:',search(),value=TRUE)  # Comment below by danielson
# e.g. ("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"
Sowmya S. Manian
  • 3,723
  • 3
  • 18
  • 30
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • I like the answer. But, lets say there are 100 packages loaded using `library`. Now to see the loaded packages list, I say `search()` on console. I can not sit and write 100 of them inside `c()` So I would need an answer where x is larger and gets combined very easily. – Sowmya S. Manian Sep 14 '16 at 06:33
  • @SowmyaS.Manian I agree that you need a way to get a list of the installed packages. For this, use `installed.packages()[,1]`. With regard to combining the results, I think the output I gave is reasonable. Typically, you always have to read through some documentation to choose a function or method, and your use case isn't any different AFAIK. – Tim Biegeleisen Sep 14 '16 at 06:37
  • We can also do like `x <- search()[-1]` and then slight changes inside `sapply()`. Almost solved my problem. – Sowmya S. Manian Sep 14 '16 at 06:40
  • Not installed packages but loaded ones. – Sowmya S. Manian Sep 14 '16 at 06:40
  • 2
    `x <- grep('package:',search(),value=TRUE)` in the code above? – danielson Sep 14 '16 at 06:42
  • @danielson Wow!! your command would remove the `.GlobalEnv` and `Autoloads` from the search list. Thanks!! – Sowmya S. Manian Sep 14 '16 at 06:45
  • TIm and danielson..Thank you both for getting this output achieved together. – Sowmya S. Manian Sep 14 '16 at 06:48
  • @docendodiscimus Even Amazing!! Now I'll have different versions! – Sowmya S. Manian Sep 14 '16 at 07:03
  • 2
    Why not drop `grep` and use the `pattern` arg in `ls()`? `ls("package:base", pattern = "is\\.")` – Rich Scriven Sep 14 '16 at 07:43
  • @RichScriven Wow, this too works inside `sapply()` Just did that for all loaded packages. Thank you :) – Sowmya S. Manian Sep 14 '16 at 09:00
0

Thanks to all. So here are Answers in 3 Versions to the question I posted with all the help here

Note: The priority was to search for a pattern over multiple loaded packages, you can search some other pattern instead of "is."

VERSION 1

 ## Get list of loaded packages into X 

 X <- 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 X 

 X <- .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 X 

 X <- .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\\."))
 })
Sowmya S. Manian
  • 3,723
  • 3
  • 18
  • 30