[R] Getting *types* of arguments?

Jan T. Kim jtk at cmp.uea.ac.uk
Thu Feb 17 13:32:29 CET 2005


On Thu, Feb 17, 2005 at 11:44:59AM +0100, Georg Hoermann wrote:
> short question: is there a possibility to get a list of
> arguments of a function *with* variable/parameter types?
> 
> formals() gives me the names of the parameters, but says
> nothing about the parameter type it expects (I know I can always use the 
> help function). 
> 
> I would like somthing like
> 
> $x: vector or data.frame...

I think you're looking for something that does not exist. There is no
notion of a parameter type in R in the way you have it in C or Java.
Whether or not a type is appropriate for a parameter cannot be decided
before the function call is actually carried out. If all operations
performed on the parameter turn out to be valid during function execution,
the parameter type can be considered valid (at least in a syntactic
view). Multiple types may well be syntactically valid in this sense,
and worse, it may depend on the content of a parameter rather than on
its type whether it is valid or not. For example, for

    foo <- function(x)
    {
      if (x[1] != "bar") x / 2 else x
    }

you'd have to expect something like

    $x: a numeric vector, data.frame etc. or a character with x[1] == "bar"

Clearly, no (finite) amount of syntactic analysis of a function's code can
produce such a result, such information has to be provided in the docs.

If you look for more strict typing, the methods package may be of interest,
methods of S4 classes allow that.

Best regards, Jan
-- 
 +- Jan T. Kim -------------------------------------------------------+
 |    *NEW*    email: jtk at cmp.uea.ac.uk                               |
 |    *NEW*    WWW:   http://www.cmp.uea.ac.uk/people/jtk             |
 *-----=<  hierarchical systems are for files, not for humans  >=-----*




More information about the R-help mailing list