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?
You can use inspect. getargspec() to see what arguments are accepted, and any default values for keyword arguments. inspect.
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.
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.
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.
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')]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With