Hello world, 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... Thanks in advance... Georg -- Georg Hoermann, Fachabteilung Wasserwirtschaft / Dep. Hydrology Ecosystem Research Center, Kiel University, Germany, Penguin #189476 Tel. 0431-880-1207, 0172/4315715, ICQ: 348340729, MSN: hlschorsch
Not to my knowledge. In some cases an argument to a function can take on different types, especially S3 methods. The only sure way is to read the help page (or if necessarily, the code). This is one thing that stumbles me sometimes: Some help pages describes what the argument does, but not what it needs to be. In some cases it may be obvious, but not all... Andy> From: Georg Hoermann > > Hello world, > > 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... > > Thanks in advance... > > Georg > > > -- > Georg Hoermann, Fachabteilung Wasserwirtschaft / Dep. Hydrology > Ecosystem Research Center, Kiel University, Germany, Penguin #189476 > Tel. 0431-880-1207, 0172/4315715, ICQ: 348340729, MSN: hlschorsch > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > R-project.org/posting-guide.html > >
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: cmp.uea.ac.uk/people/jtk | *-----=< hierarchical systems are for files, not for humans >=-----*
On Thu, 17 Feb 2005, Georg Hoermann wrote:> Hello world, > > short question: is there a possibility to get a list of > arguments of a function *with* variable/parameter types?Do you mean `argument' or `formal argument' or `parameter' or `variable'?> 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...No, because R's argument matching is polymorphic and many functions have arguments that accept many types, or coerce arguments to the required type. BTW, a data frame is a list which is a vector, so your example isn't very realistic and shows that `type' is not a concept you have clear. R has modes (?mode), types (?typeof) and classes (?class). Neither `vector' nor `data frame' is a type. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, stats.ox.ac.uk/~ripley University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
On Thu, 17 Feb 2005, Liaw, Andy wrote:> Not to my knowledge. In some cases an argument to a function can take on > different types, especially S3 methods. The only sure way is to read the > help page (or if necessarily, the code). > > This is one thing that stumbles me sometimes: Some help pages describes > what the argument does, but not what it needs to be. In some cases it may > be obvious, but not all...Or if they say, they say inaccurately. Often coercion is not mentioned, for example, and I keep on finding ones that are years out of date. So> or if necessarily, the codeusually is necessary. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, stats.ox.ac.uk/~ripley University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595