From ml-it-r-devel at epigenomics.com  Thu Jul  3 18:05:21 2003
From: ml-it-r-devel at epigenomics.com (Matthias Burger)
Date: Thu Jul  3 17:05:24 2003
Subject: [Rd] S4 method and S3 method with same name: potentially dangerous?
Message-ID: <news2mail-3F044631.20909@epigenomics.com>


Dear list,

yet another question on S4 method behaviour or rather its side effects:
Consider the following example (see below) where a new S4 method 'predict' is 
defined which by doing so makes 'predict' a S4 standardGeneric.
It works and simple test examples run but I'm cautious whether this is a 'silly' 
thing to do.

Is this a dangerous thing to do, already known to break existing code, or
cause unwanted side effects on the new S4 method?

Or can I savely make use of existing S3 methods with their clear and descriptive
names and define new S4 methods with the same name?


Thanks in advance for your time and effort!

   Matthias


 > version
          _
platform i686-pc-linux-gnu
arch     i686
os       linux-gnu
system   i686, linux-gnu
status
major    1
minor    7.1
year     2003
month    06
day      16
language R


 > predict
function (object, ...)
UseMethod("predict")
<environment: namespace:base>
 > getMethods("predict")
NULL
 > methods("predict")
  [1] "predict.Arima"             "predict.HoltWinters"
  [3] "predict.StructTS"          "predict.ar"
  [5] "predict.arima0"            "predict.glm"
  [7] "predict.lm"                "predict.loess"
  [9] "predict.mlm"               "predict.nls"
[11] "predict.poly"              "predict.ppr"
[13] "predict.princomp"          "predict.smooth.spline"
[15] "predict.smooth.spline.fit"
 >   setClass("RSVMState",
+            representation (
+                            kernel        = "character",
+                            rho           = "numeric",
+                            supportVector = "matrix",
+                            alpha         = "numeric",
+                            negGroup      = "integer",
+                            posGroup      = "integer"),
+            prototype(kernel        = new("character"),
+                      rho           = new("numeric"),
+                      supportVector = new("matrix"),
+                      alpha         = new("numeric"),
+                      negGroup      = new("integer"),
+                      posGroup      = new("integer"),
+                      name          = new("character"),
+                      description   = new("character")),
+           validity=NULL,
+            sealed=TRUE)
[1] "RSVMState"
 >   setMethod("predict", signature("RSVMState","numeric", "missing"), 
function(object, newdata, ...) { cat("\nobj:\n"); print(object); 
cat("\n\nnewdata;\n");print(newdata);return();})
Creating a new generic function for "predict"
[1] "predict"
 > predict
genericFunction for "predict" defined from package "base"

function (object, ...)
standardGeneric("predict")
<environment: 0x8e942a4>
Methods may be defined for arguments: object

 > getMethods("predict")
object = "ANY":
function (object, ...)
UseMethod("predict")

object = "RSVMState":
function (object, ...)
{
     .local <- function (object, newdata, ...)
     {
         cat("\nobj:\n")
         print(object)
         cat("\n\nnewdata;\n")
         print(newdata)
         return()
     }
     .local(object, ...)
}

 > methods("predict")
  [1] "predict.Arima"             "predict.HoltWinters"
  [3] "predict.StructTS"          "predict.ar"
  [5] "predict.arima0"            "predict.glm"
  [7] "predict.lm"                "predict.loess"
  [9] "predict.mlm"               "predict.nls"
[11] "predict.poly"              "predict.ppr"
[13] "predict.princomp"          "predict.smooth.spline"
[15] "predict.smooth.spline.fit"
 > rsvm <- new("RSVMState")
 > predict(rsvm, 10)

obj:
An object of class "RSVMState"
Slot "kernel":
character(0)

Slot "rho":
numeric(0)

Slot "supportVector":
<0 x 0 matrix>

Slot "alpha":
numeric(0)

Slot "negGroup":
numeric(0)

Slot "posGroup":
numeric(0)



newdata;
[1] 10
NULL




-- 
Matthias Burger

Bioinformatics R&D
Epigenomics AG                      www.epigenomics.com
Kleine Pr?sidentenstra?e 1          fax:   +49-30-24345-555
10178 Berlin Germany                phone: +49-30-24345-0


[Rd] S4 method and S3 method with same name: potentially dangerous?
Matthias Burger