| Line 19... |
Line 19... |
| 19 |
validObject(object, test = FALSE, complete = FALSE)
|
19 |
validObject(object, test = FALSE, complete = FALSE)
|
| 20 |
|
20 |
|
| 21 |
setValidity(Class, method, where = topenv(parent.frame()) )
|
21 |
setValidity(Class, method, where = topenv(parent.frame()) )
|
| 22 |
}
|
22 |
}
|
| 23 |
\arguments{
|
23 |
\arguments{
|
| 24 |
\item{object}{ Any object, but not much will happen unless the
|
24 |
\item{object}{ any object, but not much will happen unless the
|
| 25 |
object's class has a formal definition.}
|
25 |
object's class has a formal definition.}
|
| 26 |
\item{test}{ If \code{test} is \code{TRUE}, and validity fails the
|
26 |
\item{test}{logical; if \code{TRUE} and validity fails, the
|
| 27 |
function returns a vector of strings describing the problems. If
|
27 |
function returns a vector of strings describing the problems. If
|
| 28 |
\code{test} is \code{FALSE} (the default) validity failure generates
|
28 |
\code{test} is \code{FALSE} (the default) validity failure generates
|
| 29 |
an error.}
|
29 |
an error.}
|
| 30 |
\item{complete}{ If \code{TRUE}, validity methods will be applied
|
30 |
\item{complete}{logical; if \code{TRUE}, validity methods will be
|
| 31 |
recursively to any of the slots that have such methods.
|
31 |
applied recursively to any of the slots that have such methods.}
|
| 32 |
}
|
- |
|
| 33 |
\item{Class}{the name or class definition of the class whose validity
|
32 |
\item{Class}{the name or class definition of the class whose validity
|
| 34 |
method is to be set.}
|
33 |
method is to be set.}
|
| 35 |
\item{method}{a validity method; that is, either \code{NULL} or a
|
34 |
\item{method}{a validity method; that is, either \code{NULL} or a
|
| 36 |
function of one argument (the \code{object}). Like
|
35 |
function of one argument (the \code{object}). Like
|
| 37 |
\code{validObject}, the function should return \code{TRUE} if the
|
36 |
\code{validObject}, the function should return \code{TRUE} if the
|
| 38 |
object is valid, and one or more descriptive strings if any problems
|
37 |
object is valid, and one or more descriptive strings if any problems
|
| 39 |
are found. Unlike \code{validObject}, it should never generate an
|
38 |
are found. Unlike \code{validObject}, it should never generate an
|
| 40 |
error.
|
39 |
error.
|
| 41 |
}
|
40 |
}
|
| 42 |
\item{where}{the modified class definition will be stored in this
|
41 |
\item{where}{the modified class definition will be stored in this
|
| 43 |
environment.}
|
42 |
environment.}
|
| 44 |
|
43 |
|
| 45 |
Note that validity methods do not have to check validity of superclasses: the logic of \code{validObject} ensures
|
44 |
Note that validity methods do not have to check validity of
|
| 46 |
these tests are done once only. As a consequence, if one validity
|
45 |
superclasses: the logic of \code{validObject} ensures these tests are
|
| 47 |
method wants to use another, it should extract and call the method
|
46 |
done once only. As a consequence, if one validity method wants to use
|
| 48 |
from the other definition of the other class by calling
|
47 |
another, it should extract and call the method from the other
|
| 49 |
\code{\link{getValidity}}: it should \emph{not} call
|
48 |
definition of the other class by calling \code{\link{getValidity}}: it
|
| 50 |
\code{validObject}.
|
49 |
should \emph{not} call \code{validObject}.
|
| 51 |
}
|
50 |
}
|
| 52 |
\details{
|
51 |
\details{
|
| 53 |
Validity testing takes place \dQuote{bottom up}: Optionally, if\code{complete=TRUE}, the validity
|
52 |
Validity testing takes place \dQuote{bottom up}: Optionally, if
|
| 54 |
of the object's slots, if any, is tested. Then, in all cases, for each of the
|
53 |
\code{complete=TRUE}, the validity of the object's slots, if any, is
|
| 55 |
classes that this class extends (the \dQuote{superclasses}), the
|
54 |
tested. Then, in all cases, for each of the classes that this class
|
| 56 |
explicit validity method of that class is called, if one exists.
|
55 |
extends (the \dQuote{superclasses}), the explicit validity method of
|
| 57 |
Finally, the validity method of \code{object}'s class is called, if
|
56 |
that class is called, if one exists. Finally, the validity method of
|
| 58 |
there is one.
|
57 |
\code{object}'s class is called, if there is one.
|
| 59 |
|
58 |
|
| 60 |
Testing generally stops at the first stage of finding an error, except
|
59 |
Testing generally stops at the first stage of finding an error, except
|
| 61 |
that all the slots will be examined even if a slot has failed its
|
60 |
that all the slots will be examined even if a slot has failed its
|
| 62 |
validity test.
|
61 |
validity test.
|
| 63 |
|
62 |
|
| Line 71... |
Line 70... |
| 71 |
Otherwise a vector of strings describing problems found, except that
|
70 |
Otherwise a vector of strings describing problems found, except that
|
| 72 |
if \code{test} is \code{FALSE}, validity failure generates an error,
|
71 |
if \code{test} is \code{FALSE}, validity failure generates an error,
|
| 73 |
with the corresponding strings in the error message.
|
72 |
with the corresponding strings in the error message.
|
| 74 |
}
|
73 |
}
|
| 75 |
\references{
|
74 |
\references{
|
| 76 |
The R package \pkg{methods} implements, with a few exceptions, the
|
75 |
The \R package \pkg{methods} implements, with a few exceptions, the
|
| 77 |
programming interface for classes and methods in the book
|
76 |
programming interface for classes and methods in the book
|
| 78 |
\emph{Programming with Data} (John M. Chambers, Springer, 1998), in
|
77 |
\emph{Programming with Data} (John M. Chambers, Springer, 1998), in
|
| 79 |
particular sections 1.6, 2.7, 2.8, and chapters 7 and 8.
|
78 |
particular sections 1.6, 2.7, 2.8, and chapters 7 and 8.
|
| 80 |
|
79 |
|
| 81 |
While the programming interface for the \pkg{methods} package follows
|
80 |
While the programming interface for the \pkg{methods} package follows
|
| Line 118... |
Line 117... |
| 118 |
\dontshow{stopifnot(is(try(new("trackCurve", t2)), "try-error"))}
|
117 |
\dontshow{stopifnot(is(try(new("trackCurve", t2)), "try-error"))}
|
| 119 |
|
118 |
|
| 120 |
setClass("twoTrack", representation(tr1 = "track", tr2 ="track"))
|
119 |
setClass("twoTrack", representation(tr1 = "track", tr2 ="track"))
|
| 121 |
|
120 |
|
| 122 |
## validity tests are not applied recursively by default,
|
121 |
## validity tests are not applied recursively by default,
|
| 123 |
## so this object is created (invalidly)
|
122 |
## so this object is created (invalidly)
|
| 124 |
tT <- new("twoTrack", tr2 = t2)
|
123 |
tT <- new("twoTrack", tr2 = t2)
|
| 125 |
|
124 |
|
| 126 |
## A stricter test detects the problem
|
125 |
## A stricter test detects the problem
|
| 127 |
\dontrun{try(validObject(tT, complete = TRUE))}
|
126 |
\dontrun{try(validObject(tT, complete = TRUE))}
|
| 128 |
\dontshow{stopifnot(is(try(validObject(tT, complete = TRUE)), "try-error"))}
|
127 |
\dontshow{stopifnot(is(try(validObject(tT, complete = TRUE)), "try-error"))}
|