Description Usage Arguments Details Examples
This function can be used to define new S4-classes which are called Type.
They have an initialize method and in the introduced syntax init-method and
S4-class definition build a unit, hence a type. This simply captures a
typical setClass
then setMethod("initialize", ...)
pattern
where often some redundancy is introduced. The function has side effects due
to calling setClass
, setMethod
and assigning the constructor
function to the types name.
1 | lhs %type% rhs
|
lhs |
an expression of the form:
|
rhs |
the body of the initialize method as expression. It will be called
with |
Name-Type
expressions are also used in %m%. Besides this you
can formulate type unions in type expressions or the inheritance structure.
This has a side effect in that setClassUnion is called. Whenever you
write a type you can replace the name by an expression of the form:
type1 | type2
. Outside the slots or argument list of a method these
expressions have to be quoted. In this example the following expression is
evaluated for you: setClassUnion("type1ORtype2", c("type1", "type2"))
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | # This will create an S4-class named 'Test' with two slots; x = "numeric"
# and y = "list"; prototype: list(x = 1, y = list()); and an initialize
# method where some checks are performed.
Test(x = 1, y = list()) %type% {
stopifnot(.Object@x > 0)
.Object
}
# This will create an S4-class named 'Numeric' with a slot and some tests.
numeric : Numeric(metaInfo = character()) %type% {
stopifnot(length(.Object) > 0)
stopifnot(all(.Object > 0))
.Object
}
# This will create an S4-class with slots, where the constructor function has
# no defaults. All slots will allow for ANY type.
Anything(x, y ~ ANY, z = NULL) %type% .Object
## Not run:
Anything() # error because x and y are missing
## End(Not run)
# Type Unions:
'character | numeric' : Either(either ~ character | numeric) %type% .Object
Either("", 1)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.