Searching functions using grep over multiple loaded packages in R

后端 未结 2 1996
忘掉有多难
忘掉有多难 2021-01-18 03:29

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

 sapply(c         


        
相关标签:
2条回答
  • 2021-01-18 04:09

    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"
    
    0 讨论(0)
  • 2021-01-18 04:23

    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\\."))
     })
    
    0 讨论(0)
提交回复
热议问题