Find functions with specific arguments

后端 未结 1 538
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-05 11:11

How can you find the names and locations of all the functions that have a specific argument? Is there a way to find them for functions in the global environment, attached packa

相关标签:
1条回答
  • 2021-02-05 11:44

    I assume that you ask the question just to not lose Ben great answer. Here I slightly modify Ben answer to search for any argument :

    uses_arg <- function(x,arg) 
      is.function(fx <- get(x)) && 
      arg %in% names(formals(fx))
    

    For example to get function with na.rm argument :

    basevals <- ls(pos="package:base")      ## package name : here I use the base package
    basevals[sapply(basevals,uses_arg,'na.rm')]
    

    EDIT

    better to name argument of ls in conjunction with asNamespace :

    basevals  <- ls(asNamespace('base'))
    basevals[sapply(basevals,uses_arg,'na.rm')]
    
    0 讨论(0)
提交回复
热议问题