Searching functions using grep over multiple loaded packages in R

后端 未结 2 1995
忘掉有多难
忘掉有多难 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: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\\."))
     })
    

提交回复
热议问题