seemsS4Object            package:methods            R Documentation

_H_e_u_r_i_s_t_i_c _t_e_s_t _f_o_r _a_n _o_b_j_e_c_t _f_r_o_m _a_n _S_4 _c_l_a_s_s

_D_e_s_c_r_i_p_t_i_o_n:

     Returns 'TRUE' if 'object' seems to have been generated from a
     formally defined ("S4") class.

_U_s_a_g_e:

     seemsS4Object(object)

_A_r_g_u_m_e_n_t_s:

  object: Any object. 

_D_e_t_a_i_l_s:

     The 'class()'  of the object is examined for the '"package"'
     attribute included when objects are generated from an S4 class.

     This test can be fooled in at least two ways:

        1.  It will give 'TRUE' incorrectly if someone puts a
           '"package"' string attribute on the class of an S3 object. 
           Presumably unlikely.

        2.  It will give 'FALSE' incorrectly for class definitions and
           certain other objects for packages that have not been
           'INSTALL'ed since the 'seemsS4Object' was added to R. See
           the warning below.

_V_a_l_u_e:

     Always 'TRUE' or 'FALSE' for any object.

_W_a_r_n_i_n_g_s:

     One motivation for this function is to prevent standard S3 vector
     operations from being applied to S4 objects that are not vectors. 
     Note that 'seemsS4Object()' alone is _not_ that test.  One also
     needs to check that the object does not inherit from class
     '"vector"'.  See the examples.

     The existence of a class definition for the object's class is also
     not equivalent. S4 class definitions are recorded for S3 classes
     registered via 'setOldClass', but registering does not change the
     class of such objects, so they are not judged to be S4 objects
     (and should not be).

     Certain other S4 objects used to be generated without the
     '"package"' attribute in earlier builds of R, notably class
     definitions. Packages using S4 objects _must_ be reinstalled with
     a version of R recent enough to contain the 'seemsS4Object'
     function (e.g., the released R 2.3).

_E_x_a_m_p_l_e_s:

     seemsS4Object(1) # FALSE

     seemsS4Object(getClass(class(1)))  #TRUE

     ## how to test for an S4 object that is not a vector

     S4NotVector <- function(object) seemsS4Object(object) && !is(object, "vector")

     setClass("classNotNumeric", representation(x="numeric", y="numeric"))

     setClass("classWithNumeric", representation(y="numeric"), contains = "numeric")

     obj1 <- new("classNotNumeric", x=1, y=2)
     obj2 <- new("classWithNumeric", 1, y=2)

     seemsS4Object(obj1); seemsS4Object(obj2)  # TRUE, TRUE
     S4NotVector(obj1); S4NotVector(obj2)  # TRUE, FALSE

