[Rd] inconsistencies between ?class and ?UseMethod

Hervé Pagès hpages at fhcrc.org
Thu Dec 6 09:15:38 CET 2012



On 12/04/2012 09:40 PM, cberry at tajo.ucsd.edu wrote:
> Hervé Pagès <hpages at fhcrc.org> writes:
>
>> Hi,
>>
>> The 2 man pages give inconsistent description of class():
>>
>> Found in ?class:
>>
>>       If the object does not have a class attribute, it has an implicit
>>       class, ‘"matrix"’, ‘"array"’ or the result of ‘mode(x)’ (except
>>       that integer vectors have implicit class ‘"integer"’).
>>
>> Found in ?UseMethod:
>>
>>       Matrices and arrays have class ‘"matrix"’ or‘"array"’ followed
>>       by the class of the underlying vector.
>>       Most vectors have class the result of ‘mode(x)’, except that
>>       integer vectors have class ‘c("integer", "numeric")’ and real
>>       vectors have class ‘c("double", "numeric")’.
>>
>> So according to ?UseMethod, class(matrix(1:4)) should be
>> c("matrix", "integer", "numeric"), which is of course not the case:
>>
>>    > class(matrix(1:4))
>>    [1] "matrix"
>>
>> I wonder if this was ever true, and, if so, when and why it has changed.
>
>
> It still is in the sense that UseMethod and NextMethod dispatch in that
> manner on implicit classes.

Yes I understand that:

 > is.matrix(matrix(1:4))
[1] TRUE
 > is.integer(matrix(1:4))
[1] TRUE
 > is.numeric(matrix(1:4))
[1] TRUE

but then:

 > is(matrix(1:4), "matrix")
[1] TRUE
 > is(matrix(1:4), "integer")
[1] FALSE
 > is(matrix(1:4), "numeric")
[1] FALSE

and also:

 > inherits(matrix(1:4), "matrix")
[1] TRUE
 > inherits(matrix(1:4), "integer")
[1] FALSE
 > inherits(matrix(1:4), "numeric")
[1] FALSE

despite the use of the term _inherits_ in ?UseMethod (underlined, yes!)

See, the biggest challenge we are facing is to stay focused on 1 issue
at a time, and the issue I'm reporting here is that ?class is saying
something and ?UseMethod is saying something else. I'm not that
interested in discussing which one is telling the truth, and even less
how what they say is connected to other tools, that would take us way
too far ;-)

Thanks,
H.

> It is bit confusing to me that class()
> doesn't report all the implicit classes (like the first error message
> below) and inherits() only admits what class() has told.
>
> Note what the first error message below says and how the succesive calls
> for afun() work as methods are added to the generic.
>
> If you set a class attribute then the picture changes. I do not see where
> this is documented, but it looks like once a class attribute is set, the
> implicit classes go away - per the last error message.
>
>> afun <- function(x) UseMethod("afun",x)
>> afun(matrix(1L,nc=1))
> Error in UseMethod("afun", x) :
>    no applicable method for 'afun' applied to an object of class
>    "c('matrix', 'integer', 'numeric')"
>> afun.numeric <- function(x) cat("numeric",x,"\n")
>> afun(matrix(1L,nc=1))
> numeric 1
>> afun.integer <- function(x) cat("integer",x,"\n")
>> afun(matrix(1L,nc=1))
> integer 1
>> afun.matrix <- function(x) cat("matrix",x,"\n")
>> afun(matrix(1L,nc=1))
> matrix 1
>> afun.matrix <- function(x) NextMethod()
>> afun(matrix(1L,nc=1))
> integer 1
>> afun.fooey <- function(x) NextMethod()
>> my.mat <- matrix(1L,nc=1)
>> afun(my.mat)
> matrix 1
>> class(my.mat) <- "fooey"
>> afun(my.mat)
> Error in NextMethod() : no method to invoke
>>
>
>> Anyway, an update to ?UseMethod would be welcome.
>
> I wonder if "except with implicit" would improve over "with some
> interpolated" in ?class here:
>
> "...in R UseMethod dispatches on the class as returned by class (with
> some interpolated classes: see the link)"
>
> HTH,
>
> Chuck
>
>> Or, documenting
>> class() in only 1 place seems even better (more DRY principle).
>>
>> Thanks,
>> H.
>

-- 
Hervé Pagès

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: hpages at fhcrc.org
Phone:  (206) 667-5791
Fax:    (206) 667-1319



More information about the R-devel mailing list