R: Construct a Representation or a Prototype for a Class...
representation
R Documentation
Construct a Representation or a Prototype for a Class Definition
Description
These are old utility functions to construct, respectively
a list designed to represent the slots and superclasses and
a list of prototype specifications. The representation()
function is no longer useful, since the arguments slots and
contains to setClass are now recommended.
The prototype() function may still be used for the
corresponding argument, but a
simple list of the same arguments works as well.
Usage
representation(...)
prototype(...)
Arguments
...
The call to representation takes arguments that are single character
strings. Unnamed arguments are classes that a newly defined class
extends; named arguments name the explicit slots in the new class,
and specify what class each slot should have.
In the call to prototype, if an unnamed argument is
supplied, it unconditionally forms the basis for the prototype
object. Remaining arguments are taken to correspond to slots of
this object. It is an error to supply more than one unnamed argument.
Details
The representation function applies tests for the validity of
the arguments. Each must specify the name of a class.
The classes named don't have to exist when representation is
called, but if they do, then the function will check for any duplicate
slot names introduced by each of the inherited classes.
The arguments to prototype are usually named initial values
for slots, plus an optional first argument that gives the object
itself. The unnamed argument is typically useful if there is a data
part to the definition (see the examples below).
Value
The value of representation is just the list of arguments, after these have been checked
for validity.
The value of prototype is the object to be used as the
prototype. Slots will have been set consistently with the
arguments, but the construction does not use the class
definition to test validity of the contents (it hardly can, since
the prototype object is usually supplied to create the definition).
References
Chambers, John M. (2008)
Software for Data Analysis: Programming with R
Springer. (For the R version.)
Chambers, John M. (1998)
Programming with Data
Springer (For the original S4 version.)
See Also
setClass
Examples
## representation for a new class with a directly define slot "smooth"
## which should be a "numeric" object, and extending class "track"
representation("track", smooth ="numeric")
setClass("Character",representation("character"))
setClass("TypedCharacter",representation("Character",type="character"),
prototype(character(0),type="plain"))
ttt <- new("TypedCharacter", "foo", type = "character")
setClass("num1", representation(comment = "character"),
contains = "numeric",
prototype = prototype(pi, comment = "Start with pi"))
Results
R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(methods)
> png(filename="/home/ddbj/snapshot/RGM3/R_rel/result/methods/representation.Rd_%03d_medium.png", width=480, height=480)
> ### Name: representation
> ### Title: Construct a Representation or a Prototype for a Class Definition
> ### Aliases: representation prototype
> ### Keywords: programming classes
>
> ### ** Examples
>
> ## representation for a new class with a directly define slot "smooth"
> ## which should be a "numeric" object, and extending class "track"
> representation("track", smooth ="numeric")
[[1]]
[1] "track"
$smooth
[1] "numeric"
> ## Don't show:
> prev <- getClassDef("class3")
> setClass("class1", representation(a="numeric", b = "character"))
> setClass("class2", representation(a2 = "numeric", b = "numeric"))
> try(setClass("class3", representation("class1", "class2")))
Error in setIs(Class, class2, classDef = classDef, where = where) :
class "class3" cannot extend class "class2": slot in class "class3" must extend corresponding slot in class "class2": fails for b
Error in setClass("class3", representation("class1", "class2")) :
error in contained classes ("class2") for class "class3"; class definition removed from '.GlobalEnv'
> {if(is.null(prev))
+ stopifnot(!isClass("class3"))
+ else
+ stopifnot(identical(getClassDef("class3"), prev))}
> ## End(Don't show)
>
> setClass("Character",representation("character"))
> setClass("TypedCharacter",representation("Character",type="character"),
+ prototype(character(0),type="plain"))
> ttt <- new("TypedCharacter", "foo", type = "character")
> ## Don't show:
> stopifnot(identical(as(ttt, "character"), "foo"))
> ## End(Don't show)
>
> setClass("num1", representation(comment = "character"),
+ contains = "numeric",
+ prototype = prototype(pi, comment = "Start with pi"))
>
> ## Don't show:
> stopifnot(identical(new("num1"), new("num1", pi, comment = "Start with pi")))
> for(cl in c("num1", "TypedCharacter", "Character", "class2", "class1"))
+ removeClass(cl)
> ## End(Don't show)
>
>
>
>
>
>
> dev.off()
null device
1
>