Matthias Burger
2003-Jul-03 17:05 UTC
[Rd] S4 method and S3 method with same name: potentially dangerous?
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
Prof Brian Ripley
2003-Jul-03 17:31 UTC
[Rd] S4 method and S3 method with same name: potentially dangerous?
I think you have `method' confused with `generic'. predict() is an S3 generic, and you can safely allow that to be made into an S4 generic (although there will be a small performance hit). I would suggest that you add S4 methods to show() and not to print(), though. On Thu, 3 Jul 2003, Matthias Burger wrote:> 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? >-- Brian D. Ripley, ripley@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Possibly Parallel Threads
- RWeka control parameters classifiers interface
- RWeka cross-validation and Weka_control Parametrization
- shlib problems with Intel compiler
- Uninformative error msgs w/ svm.default - Error in svm.default ... y must be a vector or a factor -
- shlib problems with Intel compiler