R Slotnames

SlotNames(.) is now correct for 'signature' objects (mostly used internally in methods). On some systems, the first string comparison after a locale change would result in NA. CHANGES IN R 3.2.0 NEW FEATURES. AnyNA gains a recursive argument. This is a convenience function that sets the names on an object and returns the object. It is most useful at the end of a function definition where one is creating the object to be returned and would prefer not to store it under a name just so the names can be assigned. GRanges with slot start holding the start position of the gene, slot end holding the end position of the gene, slot names holding ensembl gene id, slot seqnames holding the chromosome location where the gene is located and slot strand holding the strinad information.

  • R/Tsparse.R defines the following functions:.ind.prep intI int2i.T.2.l.T.2.n.T2Cmat.T.2.C.
  • Depends R (= 3.2.5), lme4 (= 1.1-10), stats, methods Imports numDeriv, MASS, ggplot2 Suggests pbkrtest (= 0.4-3), tools Description Provides p-values in type I, II or III anova and summary tables for lmer model fits (cf. Lme4) via Satterthwaite's degrees of freedom method. A Kenward-Roger method is also available via the pbkrtest package.

In this article, you’ll learn everything about S4 classes in R; how to define them, create them, access their slots, and use them efficiently in your program.

R Slotnames


Unlike S3 classes and objects which lacks formal definition, we look at S4 class which is stricter in the sense that it has a formal definition and a uniform way to create objects.

This adds safety to our code and prevents us from accidentally making naive mistakes.

How to define S4 Class?

S4 class is defined using the setClass() function.

R SlotnamesSetnames

In R terminology, member variables are called slots. While defining a class, we need to set the name and the slots (along with class of the slot) it is going to have.

Example 1: Definition of S4 class

Slotnames

In the above example, we defined a new class called student along with three slots it’s going to have name, age and GPA.

There are other optional arguments of setClass() which you can explore in the help section with ?setClass.

How to create S4 objects?

S4 objects are created using the new() function.

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.

Now we can use this constructor function to create new objects.

Note above that our constructor in turn uses the new() function to create objects. It is just a wrap around.

Example 3: Creation of S4 objects using generator function

How to access and modify slot?

Just as components of a list are accessed using $, slot of an object are accessed using @.

Accessing slot

Modifying slot directly

A slot can be modified through reassignment.

Setnames In R

Modifying slots using slot() function

Similarly, slots can be access or modified using the slot() function.

Methods and Generic Functions

As in the case of S3 class, methods for S4 class also belong to generic functions rather than the class itself. Working with S4 generics is pretty much similar to S3 generics.

You can list all the S4 generic functions and methods available, using the function showMethods().

Example 4: List all generic functions

Writing the name of the object in interactive mode prints it. This is done using the S4 generic function show().

You can see this function in the above list. This function is the S4 analogy of the S3 print() function.

R Slotnames

Example 5: Check if a function is a generic function

We can list all the methods of show generic function using showMethods(show).

Example 6: List all methods of a generic function

R slotnames

How to write your own method?

We can write our own method using setMethod() helper function.

For example, we can implement our class method for the show() generic as follows.

Now, if we write out the name of the object in interactive mode as before, the above code is executed.

In this way we can write our own S4 class methods for generic functions.


  • PREVIOUS
    R S3 Class
  • NEXT
    R Reference Class