Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find functions with specific arguments

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 packages, and installed packages?

like image 767
Joshua Ulrich Avatar asked Jan 20 '13 23:01

Joshua Ulrich


People also ask

How do you find the argument of a function?

You can use inspect. getargspec() to see what arguments are accepted, and any default values for keyword arguments. inspect.

How do you make a function accept any number of arguments?

To make a function that accepts any number of arguments, you can use the * operator and then some variable name when defining your function's arguments.

How do you access function arguments in Python?

To extract the number and names of the arguments from a function or function[something] to return ("arg1", "arg2"), we use the inspect module. The given code is written as follows using inspect module to find the parameters inside the functions aMethod and foo.

When using a function the functions arguments can be specified by?

Because all function arguments have names, they can be specified using their name. Specifying an argument by its name is sometimes useful if a function has many arguments and it may not always be clear which argument is being specified. Here, our function only has one argument so there's no confusion.


1 Answers

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')]
like image 59
agstudy Avatar answered Oct 15 '22 08:10

agstudy