Get list of arguments for particular method

Hi guys,

Just wondering if there is an easy way to extract the argument names of a particular method? For example the following function foo has methods for list or data.frame objects each with different arguments:

# Set generic for foo()
setGeneric(name = "foo", def = function(x, ...) {standardGeneric("foo")})

# list method for 'foo'
setMethod(foo, signature = "list", definition = function(x, y, z){
# Do something here
})

# data.frame method for 'foo'
setMethod(foo, signature = "data.frame", definition = function(x, y, z, a, b){
# Do something here
})

I would like to return name of the arguments for the data.frame method, i.e. expect arguments 'x', 'y', 'z', 'a' and 'b'.

I have tried calling args() and argsAnywhere() on foo but it returns the definition for the generic - function(x,...). Is there an easy way to get a list of arguments for a particular method?

Thanks in advance for your help.

Dillon

1 Like

The solution is to define the function under a new name (e.g. foo.list) and then calling args() or formals() on the function rather than the generic works as expected. See below:

# Set generic for foo()
setGeneric(name = "foo", def = function(x, ...) {standardGeneric("foo")})

# Create function describing the list method
foo.list <- function(x,y,z,a,b){}

# Assign this function as a method for the `foo()` generic
setMethod(foo, signature = "list", definition = foo.list)

Calling formals(foo.list) returns a list of named arguments as expected.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.