| new_class | R Documentation |
A class specifies the properties (data) that each of its objects will possess. The class, and its parent, determines which method will be used when an object is passed to a generic.
Learn more in vignette("classes-objects")
new_class(
name,
parent = S7_object,
package = topNamespaceName(parent.frame()),
properties = list(),
abstract = FALSE,
constructor = NULL,
validator = NULL
)
new_object(`_parent`, ...)
name |
The name of the class, as a string. (We recommend using CamelCase for S7 class names, but it is not required.) The result of calling |
parent |
The parent class to inherit behavior from. There are four options:
When defining a class in a package with an S7 parent from another package,
the parent's properties are captured when your class is defined, but the
default constructor calls the parent's installed constructor at run time.
Parent arguments are forwarded through |
package |
Package name. This is automatically resolved if the class is
defined in a package, and Note, if the class is intended for external use, the constructor should be
exported. Learn more in |
properties |
A named list specifying the properties (data) that
belong to each instance of the class. Each element of the list can
either be a type specification (processed by |
abstract |
Is this an abstract class? An abstract class can not be instantiated. |
constructor |
The constructor function. In most cases, you can rely on the default constructor, which will generate a function with one argument for each property. A custom constructor should call |
validator |
A function taking a single argument, The job of a validator is to determine whether the object is valid, i.e. if the current property values form an allowed combination. The types of the properties are always automatically validated so the job of the validator is to verify that the values of individual properties are ok (i.e. maybe a property should have length 1, or should always be positive), or that the combination of values of multiple properties is ok. It is called after construction and whenever any property is set. The validator should return See |
_parent, ... |
Parent object and named properties used to construct the object. When the inheritance chain ends at S7_object, only
attributes corresponding to its properties are copied from As a convenience, if |
A object constructor, a function that can be used to create objects of the given class.
new_class() can use an S4 class definition or class generator as its
parent. S4 parent slots become S7 properties, the class is registered with
S4 automatically, and S4 generics can dispatch through the S4 parent.
S7 objects that directly extend S4 are represented as S3 old-class objects,
so isS4() is FALSE even though methods::is() and S4 dispatch see the
S4 parent. S4 classes can extend S7 classes by calling
S4_contains() in methods::setClass(contains = ).
See vignette("compatibility") for examples and caveats.
# Create an class that represents a range using a numeric start and end
Range := new_class(
properties = list(
start = class_numeric,
end = class_numeric
)
)
r <- Range(start = 10, end = 20)
r
# get and set properties with @
r@start
r@end <- 40
r@end
# S7 automatically ensures that properties are of the declared types:
try(Range(start = "hello", end = 20))
# But we might also want to use a validator to ensure that start and end
# are length 1, and that start is < end
Range := new_class(
properties = list(
start = class_numeric,
end = class_numeric
),
validator = function(self) {
if (length(self@start) != 1) {
"@start must be a single number"
} else if (length(self@end) != 1) {
"@end must be a single number"
} else if (self@end < self@start) {
"@end must be great than or equal to @start"
}
}
)
try(Range(start = c(10, 15), end = 20))
try(Range(start = 20, end = 10))
r <- Range(start = 10, end = 20)
try(r@start <- 25)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.