How to define the subset operators for a S4 class?

Kyle Brandt picture Kyle Brandt · Jun 9, 2012 · Viewed 7.3k times · Source

I am having trouble figuring out the proper way to define the [, $, and [[ subset operators for an S4 class.

Can anyone provide me with a basic example of defining these three for an S4 class?

Answer

Martin Morgan picture Martin Morgan · Jun 9, 2012

Discover the generic so that we know what we are aiming for

> getGeneric("[")
standardGeneric for "[" defined from package "base"

function (x, i, j, ..., drop = TRUE) 
standardGeneric("[", .Primitive("["))
<bytecode: 0x32e25c8>
<environment: 0x32d7a50>
Methods may be defined for arguments: x, i, j, drop
Use  showMethods("[")  for currently available ones.

Define a simple class

setClass("A", representation=representation(slt="numeric"))

and implement a method

setMethod("[", c("A", "integer", "missing", "ANY"),
    ## we won't support subsetting on j; dispatching on 'drop' doesn't
    ## make sense (to me), so in rebellion we'll quietly ignore it.
    function(x, i, j, ..., drop=TRUE)
{
    ## less clever: update slot, return instance
    ## x@slt = x@slt[i]
    ## x
    ## clever: by default initialize is a copy constructor, too
    initialize(x, slt=x@slt[i])
})

In action:

> a = new("A", slt=1:5)
> a[3:1]
An object of class "A"
Slot "slt":
[1] 3 2 1

There are different strategies for supporting the (implicitly) many signatures, for instance you'd likely also want to support logical and character index values, possibly for both i and j. The most straight-forward is a "facade" pattern where each method does some preliminary coercion to a common type of subset index, e.g., integer to allow for re-ordering and repetition of index entries, and then uses callGeneric to invoke a single method that does the work of subsetting the class.

There are no conceptual differences for [[, other than wanting to respect the semantics of returning the content rather than another instance of the object as implied by [. For $ we have

> getGeneric("$")
standardGeneric for "$" defined from package "base"

function (x, name) 
standardGeneric("$", .Primitive("$"))
<bytecode: 0x31fce40>
<environment: 0x31f12b8>
Methods may be defined for arguments: x
Use  showMethods("$")  for currently available ones.

and

setMethod("$", "A",
    function(x, name)
{
    ## 'name' is a character(1)
    slot(x, name)
})

with

> a$slt
[1] 1 2 3 4 5