From mtmorgan at fhcrc.org  Tue Sep  7 15:28:30 2010
From: mtmorgan at fhcrc.org (Martin Morgan)
Date: Tue, 07 Sep 2010 06:28:30 -0700
Subject: [Rd] Dispatch method on S3 or S4 class
In-Reply-To: <20100907150024.BIQ94758@gimr.garvan.unsw.edu.au>
References: <20100907150024.BIQ94758@gimr.garvan.unsw.edu.au>
Message-ID: <4C863DFE.1010506@fhcrc.org>

On 09/06/2010 10:00 PM, Dario Strbenac wrote:
> Hello,
>
> I've been attempting to make a generic method that dispatches on the first argument, which can be either an S3 or an S4 class. This is as far as I've gotten. Any suggestions about what to try next ?
>
> library(aroma.affymetrix)
> library(GenomicRanges)
>
> setGeneric("analyse", function(x, y, ...) standardGeneric("analyse"))
>
> setMethodS3("analyse", "AffymetrixCelSet", function(x, y, z, ...)
> {
> 	x;
> 	UseMethod("analyse")
> }
> )
>
> setGeneric("analyse")
>
> setMethod("analyse", "GRangesList", function(x, y, a, b, c)
> {
> 	x;
> }
> )
I think (no testing on my end) you want

setOldClass("AffymetrixCelSet")

setGeneric("analyse", function(x, y, ...) standardGeneric("analyse"))

setMethod(analyse, "AffymetrixCelSet", function(x, y, z, ...)
{
    cat("AffymetrixCelSet\n")
    x
})

setMethod(analyse, "GRangesList", function(x, y, a, b, c)
{
    cat("GRangesList\n")
    x
})

and then by way of reproducible example

> x = analyse(structure(list(), class="AffymetrixCelSet"))
AffymetrixCelSet
> y = analyse(GRangesList())
GRangesList


Martin
>
> Thanks,
>        Dario.
>
> --------------------------------------
> Dario Strbenac
> Research Assistant
> Cancer Epigenetics
> Garvan Institute of Medical Research
> Darlinghurst NSW 2010
> Australia
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



[Rd] Dispatch method on S3 or S4 class
Martin Morgan