Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Example of Using an S3 Class in a S4 Object

Tags:

methods

r

s4

I want to include an RODBC connection as part of S4 object. It looks like RODBC is S3. For example:

setClass(
  Class="Node",
  representation=representation(
    nodeName = "character",
    connection = "RODBC"
  )                    
)

Throws undefined slot classes. It looks like I want to use setOldClass, but I am having trouble figuring out how to use it. Assuming I do want setOldClass, How would I use setOldClass so that I can include my RODBC connection as slot to my Node class?

like image 870
Kyle Brandt Avatar asked Jan 30 '12 16:01

Kyle Brandt


People also ask

How does the S4 class differ from S3 class?

The S3 and S4 software in R are two generations implementing functional object-oriented programming. S3 is the original, simpler for initial programming but less general, less formal and less open to validation. The S4 formal methods and classes provide these features but require more programming.

How do you create S4 objects How can you check an object is S4 object?

Example 2: Creation of S4 object We can check if an object is an S4 object through the function isS4() . The function setClass() returns a generator function. This generator function (usually having same name as the class) can be used to create new objects. It acts as a constructor.

What is an S4 object?

The S4 system in R is a system for object oriented programing. Confusingly, R has support for at least 3 different systems for object oriented programming: S3, S4 and S5 (also known as reference classes).

What is an S3 class?

An S3 class is the most prevalent and used class in R programming. It is easy to implement this class and most of the predefined classes are of this type. An S3 object is basically a list with its class attributes assigned some names. And the member variable of the object created is the components of the list.

What is an S3 object?

Basically, a list with its class attribute set to some class name, is an S3 object. The components of the list become the member variables of the object. Following is a simple example of how an S3 object of class student can be created.

What is the difference between S3 and S4 classes in R?

More specifically, S4 has setter and getter functions for methods and generics. As compared to the S3 class, S4 can be able to facilitate multiple dispatches. Let us see the above-discussed classes of R by creating them and discuss with an example. An S4 class is defined using the set class function.

What is the difference between S3 and S4 in Java?

S4 Class is stricter, conventional, and closely related to Object-Oriented concepts. The classes are represented by the formal class definitions of S4. More specifically, S4 has setter and getter functions for methods and generics. As compared to the S3 class, S4 can be able to facilitate multiple dispatches.

What is an S3 class in Python?

S3 class has no formal, predefined definition. Basically, a list with its class attribute set to some class name, is an S3 object. The components of the list become the member variables of the object. Following is a simple example of how an S3 object of class student can be created.


1 Answers

Although the documentation is quite involved for this function, if all you need to do include the class in a slot it is as simple as:

setOldClass("RODBC")

setClass(
  Class="Node",
  representation=representation(
    nodeName = "character",
    connection = "RODBC"
  )                    
)

This is also what you would use for reference classes.

like image 136
Kyle Brandt Avatar answered Oct 16 '22 06:10

Kyle Brandt