validObject             package:methods             R Documentation

_T_e_s_t _t_h_e _V_a_l_i_d_i_t_y _o_f _a_n _O_b_j_e_c_t

_D_e_s_c_r_i_p_t_i_o_n:

     The validity of 'object' related to its class definition is
     tested.  If the object is valid, 'TRUE' is returned; otherwise,
     either a vector of strings describing validity failures is
     returned, or an error is generated (according to whether 'test' is
     'TRUE').

     The function 'setValidity'  sets the validity method of a class
     (but more normally, this method will be supplied as the 'validity'
     argument to 'setClass').  The method should be a function of one
     object that returns 'TRUE' or a description of the non-validity.

_U_s_a_g_e:

     validObject(object, test)

      setValidity(Class, method, where = topenv(parent.frame()) )

_A_r_g_u_m_e_n_t_s:

  object: Any object, but not much will happen unless the object's
          class has a formal definition.

    test: If 'test' is 'TRUE', and validity fails the function returns
          a vector of strings describing the problems.  If 'test' is
          'FALSE' (the default) validity failure generates an error.

   Class: the name or class definition of the class whose validity
          method is to be set.

  method: a validity method;  that is, either 'NULL' or a function of
          one argument (the 'object').  Like 'validObject', the
          function should return 'TRUE' if the object is valid, and one
          or more descriptive strings if any problems are found. 
          Unlike 'validObject', it should never generate an error. 

   where: the modified class definition will be stored in this
          environment.


     Note that validity methods do not have to check validity of any
     slots or superclasses:  the logic of 'validObject' ensures these
     tests are done once only.  As a consequence, if one validity
     method wants to use another, it should extract and call the method
     from the other definition of the other class by calling
     'getValidity':  it should _not_ call 'validObject'.

_D_e_t_a_i_l_s:

     Validity testing takes place "bottom up":  first the validity of
     the object's slots, if any, is tested.  Then for each of the
     classes that this class extends (the "superclasses"), the explicit
     validity method of that class is called, if one exists. Finally,
     the validity method of 'object''s class is called, if there is
     one.

     Testing generally stops at the first stage of finding an error,
     except that all the slots will be examined even if a slot has
     failed its validity test.

_V_a_l_u_e:

     'validObject' returns 'TRUE' if the object is valid. Otherwise a
     vector of strings describing problems found, except that if 'test'
     is 'FALSE', validity failure generates an error, with the
     corresponding strings in the error message.

_R_e_f_e_r_e_n_c_e_s:

     The R package 'methods' implements, with a few exceptions, the
     programming interface for classes and methods in the book
     _Programming with Data_ (John M. Chambers, Springer, 1998), in
     particular sections 1.6, 2.7, 2.8, and chapters 7 and 8.

     While the programming interface for the 'methods' package follows
     the reference, the R software is an original implementation, so
     details in the reference that reflect the S4 implementation may
     appear differently in R.  Also, there are extensions to the
     programming interface developed more recently than the reference. 
     For a discussion of details and ongoing development, see the web
     page <URL: http://developer.r-project.org/methodsPackage.html> and
     the pointers from that page.

_S_e_e _A_l_s_o:

     'setClass'.

_E_x_a_m_p_l_e_s:

     setClass("track",
               representation(x="numeric", y = "numeric"))
     t1 <- new("track", x=1:10, y=sort(rnorm(10)))
     ## A valid "track" object has the same number of x, y values
     validTrackObject <- function(x){
         if(length(x@x) == length(x@y)) TRUE
         else paste("Unequal x,y lengths: ", length(x@x), ", ", length(x@y),
         sep="")
     }
     ## assign the function as the validity method for the class
     setValidity("track", validTrackObject)
     ## t1 should be a valid "track" object
     validObject(t1)
     ## Now we do something bad
     t1@x <- 1:20
     ## This should generate an error
     ## Not run: try(validObject(t1))

