Re: [R] variable names when using S3 methods

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Mon, 28 Apr 2008 13:43:19 +0100 (BST)

options(warnPartialMatchArgs=TRUE) is available to those who do regrets, e.g.

> options(warnPartialMatchArgs=TRUE)
> fooA("hi",m=1)
foo.default: m is 1
Warning message:
In fooA("hi", m = 1) : partial argument match of 'm' to 'model'

On Mon, 28 Apr 2008, Peter Dalgaard wrote:

> Aaron Rendahl wrote:
>> I'm seeing some funny behavior when using methods (the older S3 type)
>> and having variables that start with the same letter. I have a vague
>> recollection of reading something about this once but now can't seem
>> to find anything in the documentation. Any explanation, or a link to
>> the proper documentation, if it does exist, would be appreciated.
>>
>> Thanks, Aaron Rendahl
>> University of Minnesota School of Statistics
>>
>>
>> # set up two function that both use method "foo" but with different
>> variable names
>> fooA<-function(model,...)
>> UseMethod("foo")
>> fooB<-function(Bmodel,...)
>> UseMethod("foo")
>>
>> # now set up two methods (default and character) that have an
>> additional variable
>> foo.character <- function(model, m=5,...)
>> cat("foo.character: m is", m, "\n")
>> foo.default <- function(model, m=5,...)
>> cat("foo.default: m is", m, "\n")
>>
>> # both of these use foo.character, as expected
>> fooA("hi")
>> fooB("hi")
>>
>> # but here, fooA uses foo.default instead
>> fooA("hi",m=1)
>> fooB("hi",m=1)
>>
>> # additionally, these use foo.character, as expected
>> fooA("hi",1)
>> fooA(model="hi",m=1)
> This is partial matching on argument names, a feature regretted by its
> designers, but a feature nevertheless.
>
> It has nothing to do with S3 as such, it is just that fooA("hi",m=1)
> matches "m" to "model" and believes that you meant fooA("hi", model=1).
>
> It often bites people in S3 methods though, because in most other cases,
> you can put the arguments after "..." where they require exact matching
> (consider the naming forms of c() and cbind(), for instance).
>
> --
> O__ ---- Peter Dalgaard ุster Farimagsgade 5, Entr.B
> c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K
> (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
> ~~~~~~~~~~ - (p.dalgaard_at_biostat.ku.dk) FAX: (+45) 35327907
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

-- 
Brian D. Ripley,                  ripley_at_stats.ox.ac.uk
Professor of Applied Statistics,  http://www.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


______________________________________________ R-help_at_r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.

Received on Mon 28 Apr 2008 - 12:47:34 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 28 Apr 2008 - 13:30:32 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive