R Slotnames
Loading the Matrix namespace “overloads” as.matrix and as.array in the base namespace by the equivalent of function(x) as(x, 'matrix').Consequently, as.matrix(m) or as.array(m) will properly work when m inherits from the 'Matrix' class - also for functions in package base and other packages. E.g., apply or outer can therefore be applied to 'Matrix' matrices. Estimate DCC Model dcc fit =dcc.fit = dccfit(dcc garch11 spec data =(dcc.garch11.spec, data = MSFT GSPC retMSFT.GSPC.ret) Iter: 1 fn: 2261.1651 Pars: 0.02425 0.96193. R slotNames(mniRL) 1. Analysis was undertaken to compare every voxel in the entire volume of the TRs to its matching voxel in the RR using R 456.
slot | returns the contents of the specified slot for slot(). |
slotNames | returns the character vector of the names of the slots for an S4 class. If x is a string giving the name of an S4 class, it returns the slot names for that class. If x is an S4 object, it returns the slot names for that object's class. If x is a classRepresentation object, it returns the slot names from the specified class, rather than the slots in the classRepresentation class. |
.slotNames | returns the character vector of the names of the slots for an S4 class. This is the same as slotNames, except that if x is a classRepresentation object, slotNames returns the slot names from the classRepresentation class itself. |
getSlots | returns a named character vector, whose elements are the classes of the slots, and whose names are the names of the slots. x must be either a string giving the name of an S4 class, or a classRepresentation object. |
.hasSlot | returns TRUE if name is one of the attribute names of object.This detects slots defined in the object class, as well as other attributes added to the object. |
Contents
We examine how to create S4 classes. It is assumed that you arefamiliar with the basic data types and scripting (Introduction to Programming).
The S4 approach differs from the S3 approach to creating a class inthat it is a more rigid definition. The idea is that an object iscreated using the setClass command. The command takes a number ofoptions. Many of the options are not required, but we make use ofseveral of the optional arguments because they represent goodpractices with respect to object oriented programming.
We first construct a trivial, contrived class simply to demonstratethe basic idea. Next we demonstrate how to create a method for an S4class. This example is a little more involved than what we saw in thesection on S3 classes.
In this example, the name of the class is FirstQuadrant, and theclass is used to keep track of an (x,y) coordinate pair in the firstquadrant. There is a restriction that both values must be greater thanor equal to zero. There are two data elements, called slots, andthey are called x and y. The default values for the coordinate isthe origin, x=0 and y=0.
Note that the way to access one of the data elements is to use the “@”symbol. An example if given below. In the example three elementsof the class defined above are created. The first uses the defaultvalues for the slots, the second overrides the defaults, and finallyan attempt is made to create a coordinate in the second quadrant.
In the next example we create a method that is associated with theclass. The method is used to set the values of a coordinate. The firststep is to reserve the name using the setGeneric command, and thenthe setMethod command is used to define the function to be calledwhen the first argument is an object from the FirstQuadrant class.
It is important to note that R generally passes objects as values. Forthis reason the methods defined above return the updated object. Whenthe method is called, it is used to replace the former object with theupdated object.
Note that the validity function given in the original classdefinition is not called. It is called when an object is firstdefined. It can be called later, but only when an explicit request ismade using the validObject command.
An S4 class is created using the setClass() command. At a minimumthe name of the class is specified and the names of the data elements(slots) is specified. There are a number of other options, and just asa matter of good practice we also specify a function to verify thatthe data is consistent (validation), and we specify the defaultvalues (the prototype). In the last section of this page,S4 inheritance, we include an additionalparameter used to specify a class hierarchy.
Setnames In R
In this section we look at another example, and we examine some of thefunctions associated with S4 classes. The example we define will beused to motivate the use of methods associated with a class, and itwill be used to demonstrate inheritance later. The idea is that wewant to create a program to simulate a cellular automata model of apredator-prey system.
We do not develop the whole code here but concentrate on the datastructures. In particular we will create a base class for theagents. In the next section we will create the basic methods for theclass. In the inheritance section we will discuss how to build on theclass to create different predators and different prey species. Thebasic structure of the class is shown in Figure 1.
Figure 1.
The methods for this class are defined in the following section. Herewe define the class and its slots, and the code to define the class isgiven below:
Now that the code to define the class is given we can create an objectwhose class is Agent.
Before we define the methods for the class a number of additionalcommands are explored. The first set of functions explored are theis.object and the isS4 commands. The is.object commanddetermines whether or not a variable refers to an object. The isS4command determines whether or not the variable is an S4 object. Thereason both are required is that the isS4 command alone cannotdetermine if a variable is an S3 object. You need to determine if thevariable is an object and then decide if it is S4 or not.
The next set of commands are used to get information about the dataelements, or slots, within an object. The first is the slotNamescommand. This command can take either an object or the name of aclass. It returns the names of the slots associated with the class asstrings.
The getSlots command is similar to the slotNames command. It takesthe name of a class as a string. It returns a vector whose entriesare the types associated with the slots, and the names of the entriesare the names of the slots.
The next command examined is the getClass command. It has twoforms. If you give it a variable that is an S4 class it returns a listof slots for the class associated with the variable. If you give it acharacter string with the name of a class it gives the slots and theirdata types.
The final command examined is the slot command. It can be used toget or set the value of a slot in an object. It can be used in placeof the “@” operator.
We now build on the Agent class defined above. Once the class and itsdata elements are defined we can define the methods associated withthe class. The basic idea is that if the name of a function has notbeen defined, the name must first be reserved using the setGenericfunction. The setMethod can then be used to define which function iscalled based on the class names of the objects sent to it.
We define the methods associated with the Agent method given in theprevious section. Note that the validity function for an object isonly called when it is first created and when an explicit call to thevalidObject function is made. We make use of the validObjectcommand in the methods below that are used to change the value of adata element within an object.
With these definitions the data elements are encapsulated and can beaccessed and set using the methods given above. It is generally goodpractice in object oriented programming to keep your data private andnot show them to everybody willy nilly.
The last topic examined is the idea of overloading functions. In theexamples above the signature is set to a single element. The signatureis a vector of characters and specifies the data types of the argumentlist for the method to be defined. Here we create two new methods. Thename of the method is resetActivity, and there are two versions.
R Slotnames
The first version accepts two arguments whose types are Agent andlogical. This version of the method will set the activity slot to agiven value. The second version accepts two arguments whose types areAgent and numeric. This version will set the activity to TRUE andthen set the energy level to the value passed to it. Note that thenames of the variables in the argument list must be exactly the same.
This definition of the function yields two options for theresetActivity function. The decision to determine which function tocall depends on two arguments and their type. For example, if thefirst argument is from the Agent class and the second is a value ofTRUE or FALSE, then the first version of the function iscalled. Otherwise, if the second argument is a number the secondversion of the function is called.
A class’ inheritance hiearchy can be specified when the class isdefined using the contains option. The contains option is a vectorthat lists the classes the new class inherits from. In the followingexample we build on the Agent class defined in the previoussection. The idea is that we need agents that represent a predator andtwo prey. We will focus on two predators for this example.
The hierarchy for the classes is shown inFigure 2.. In this example we have one Preyclass that is derived from the Agent class. There are two predatorclasses, Bobcat and Lynx. The Bobcat class is derived from the Agentclass, and the Lynx class is derived from the Bobcat class. We willkeep this very simple, and the only methods associated with the newclasses is a move method. For our purposes it will only print out amessage and set the values of the position and velocity to demonstratethe order of execution of the methods associated with the classes.
Figure 2.
The first step is to create the three new classes.
The inheritance is specified using the contains option in thesetClass command. Note that this can be a vector allowing formultiple inheritance. We choose not to use that to keep thingssimpler. If you are feeling like you need more self-loathing in yourlife you should try it out and experiment.
Next we define a method, move, for the new classes. We will includemethods for the Agent, Prey, Bobcat, and Lynx classes. The methods donot really do anything but are used to demonstrate the idea of howmethods are executed.
There are a number of things to note. First each method calls thecallNextMethod command. This command will execute the next versionof the same method for the previous class in the hierarchy. Note thatI have included the arguments (in the same order) as those called bythe original function. Also note that the function returns a copy ofthe object and is used to update the object passed to the originalfunction.
Another thing to note is that the methods associated with the Lync,Bobcat, and Agent classes arbitrarily change the values of theposition, velocity, and activity for the given object. This is done todemonstrate the changes that take place and reinforce the necessityfor using the callNextMethod function the way it is used here.
Finally, it should be noted that the validObject command is calledin every method. You should try adding a print statement in thevalidity function. You might find that the order is a bit odd. Youshould experiment with this and play with it. There are times you donot get the expected results so be careful!
We now give a brief example to demonstrate the order that thefunctions are called. In the example we create a Bobcat object andthen call the move method. We next create a Lynx object and do thesame. We print out the slots for both agents just to demonstrate thevalues that are changed.