[R] 'class(.) == **' [was 'Call to a function']

Martin Maechler maechler at stat.math.ethz.ch
Wed Jun 24 09:20:57 CEST 2015


>>>>> Steve Taylor <steve.taylor at aut.ac.nz>
>>>>>     on Wed, 24 Jun 2015 00:56:26 +0000 writes:

    > Note that objects can have more than one class, in which case your == and %in% might not work as expected.  

    > Better to use inherits().

    > cheers,
    > Steve

Yes indeed, as Steve said, really do!  

The use of   (class(.) == "....")   it is error prone and
against the philosophy of classes (S3 or S4 or ..) in R :

Classes can "extend" other classes or "inherit" from them;
S3 examples in "base R"  are
 - glm() objects which are "glm"
   but also inherit from "lm"
 - multivariate time-series are "mts" and "ts"
 - The time-date objects  POSIXt , POSIXct, POSIXlt

==> do work  with  inherits(<obj>, <class))
or  possibly       is( <obj>, <class>)


We've seen this use of	

     class(.) == ".."    (or '!=" or  %in% ...)

in too many places;  though it may work fine in your test cases,
it is wrong to be used in generality e.g. inside a function you
provide for more general use,
and is best  replaced with the use of inherits() / is()
everywhere  "out of principle".

Martin Maechler
ETH Zurich



More information about the R-help mailing list