| 16446 |
jmc |
1 |
\name{validObject}
|
|
|
2 |
\alias{validObject}
|
|
|
3 |
\alias{setValidity}
|
|
|
4 |
\title{ Test the Validity of an Object }
|
|
|
5 |
\description{
|
|
|
6 |
The validity of \code{object} related to its class definition is
|
|
|
7 |
tested. If the object is valid, \code{TRUE} is returned; otherwise,
|
|
|
8 |
either a vector of strings describing validity failures is returned,
|
|
|
9 |
or an error is generated (according to whether \code{test} is
|
|
|
10 |
\code{TRUE}).
|
|
|
11 |
|
| 24232 |
jmc |
12 |
The function \code{setValidity} sets
|
|
|
13 |
the validity method of a class (but more normally, this method will
|
|
|
14 |
be supplied as the \code{validity} argument to \code{\link{setClass}}). The method should be a function of one
|
| 16446 |
jmc |
15 |
object that returns \code{TRUE} or a description of the non-validity.
|
|
|
16 |
}
|
|
|
17 |
\usage{
|
|
|
18 |
validObject(object, test)
|
|
|
19 |
|
| 26530 |
maechler |
20 |
setValidity(Class, method, where = topenv(parent.frame()) )
|
| 16446 |
jmc |
21 |
}
|
|
|
22 |
\arguments{
|
|
|
23 |
\item{object}{ Any object, but not much will happen unless the
|
|
|
24 |
object's class has a formal definition.}
|
|
|
25 |
\item{test}{ If \code{test} is \code{TRUE}, and validity fails the
|
|
|
26 |
function returns a vector of strings describing the problems. If
|
|
|
27 |
\code{test} is \code{FALSE} (the default) validity failure generates
|
|
|
28 |
an error.}
|
| 26530 |
maechler |
29 |
\item{Class}{the name or class definition of the class whose validity
|
|
|
30 |
method is to be set.}
|
|
|
31 |
\item{method}{a validity method; that is, either \code{NULL} or a
|
| 19029 |
hornik |
32 |
function of one argument (the \code{object}). Like
|
|
|
33 |
\code{validObject}, the function should return \code{TRUE} if the
|
|
|
34 |
object is valid, and one or more descriptive strings if any problems
|
|
|
35 |
are found. Unlike \code{validObject}, it should never generate an
|
|
|
36 |
error.
|
| 21345 |
jmc |
37 |
}
|
| 26530 |
maechler |
38 |
\item{where}{the modified class definition will be stored in this
|
|
|
39 |
environment.}
|
| 16446 |
jmc |
40 |
|
| 26530 |
maechler |
41 |
Note that validity methods do not have to check validity of any
|
|
|
42 |
slots or superclasses: the logic of \code{validObject} ensures
|
|
|
43 |
these tests are done once only. As a consequence, if one validity
|
|
|
44 |
method wants to use another, it should extract and call the method
|
|
|
45 |
from the other definition of the other class by calling
|
|
|
46 |
\code{\link{getValidity}}: it should \emph{not} call
|
|
|
47 |
\code{validObject}.
|
| 16446 |
jmc |
48 |
}
|
|
|
49 |
\details{
|
| 25118 |
hornik |
50 |
Validity testing takes place \dQuote{bottom up}: first the validity
|
|
|
51 |
of the object's slots, if any, is tested. Then for each of the
|
|
|
52 |
classes that this class extends (the \dQuote{superclasses}), the
|
|
|
53 |
explicit validity method of that class is called, if one exists.
|
|
|
54 |
Finally, the validity method of \code{object}'s class is called, if
|
|
|
55 |
there is one.
|
| 16446 |
jmc |
56 |
|
|
|
57 |
Testing generally stops at the first stage of finding an error, except
|
|
|
58 |
that all the slots will be examined even if a slot has failed its
|
|
|
59 |
validity test.
|
|
|
60 |
}
|
|
|
61 |
\value{
|
| 19029 |
hornik |
62 |
\code{validObject} returns \code{TRUE} if the object is valid.
|
|
|
63 |
Otherwise a vector of strings describing problems found, except that
|
|
|
64 |
if \code{test} is \code{FALSE}, validity failure generates an error,
|
|
|
65 |
with the corresponding strings in the error message.
|
| 16446 |
jmc |
66 |
}
|
|
|
67 |
\references{
|
| 25118 |
hornik |
68 |
The R package \pkg{methods} implements, with a few exceptions, the
|
|
|
69 |
programming interface for classes and methods in the book
|
|
|
70 |
\emph{Programming with Data} (John M. Chambers, Springer, 1998), in
|
|
|
71 |
particular sections 1.6, 2.7, 2.8, and chapters 7 and 8.
|
| 16446 |
jmc |
72 |
|
| 25118 |
hornik |
73 |
While the programming interface for the \pkg{methods} package follows
|
|
|
74 |
the reference, the R software is an original implementation, so
|
|
|
75 |
details in the reference that reflect the S4 implementation may appear
|
| 21345 |
jmc |
76 |
differently in R. Also, there are extensions to the programming
|
|
|
77 |
interface developed more recently than the reference. For a
|
| 26530 |
maechler |
78 |
discussion of details and ongoing development, see the web page
|
| 21345 |
jmc |
79 |
\url{http://developer.r-project.org/methodsPackage.html} and the
|
|
|
80 |
pointers from that page.
|
| 16446 |
jmc |
81 |
}
|
|
|
82 |
\seealso{ \code{\link{setClass}}. }
|
|
|
83 |
|
|
|
84 |
\examples{
|
| 26000 |
ripley |
85 |
setClass("track",
|
|
|
86 |
representation(x="numeric", y = "numeric"))
|
|
|
87 |
t1 <- new("track", x=1:10, y=sort(rnorm(10)))
|
|
|
88 |
## A valid "track" object has the same number of x, y values
|
|
|
89 |
validTrackObject <- function(x){
|
| 16446 |
jmc |
90 |
if(length(x@x) == length(x@y)) TRUE
|
|
|
91 |
else paste("Unequal x,y lengths: ", length(x@x), ", ", length(x@y),
|
|
|
92 |
sep="")
|
|
|
93 |
}
|
| 26000 |
ripley |
94 |
## assign the function as the validity method for the class
|
|
|
95 |
setValidity("track", validTrackObject)
|
|
|
96 |
## t1 should be a valid "track" object
|
|
|
97 |
validObject(t1)
|
|
|
98 |
## Now we do something bad
|
|
|
99 |
t1@x <- 1:20
|
|
|
100 |
## This should generate an error
|
|
|
101 |
\dontrun{try(validObject(t1))}
|
|
|
102 |
\dontshow{stopifnot(is(try(validObject(t1)), "try-error"))}
|
|
|
103 |
}
|
| 16446 |
jmc |
104 |
|
| 17454 |
jmc |
105 |
\keyword{programming}
|
|
|
106 |
\keyword{classes}
|