[R] Generic Functions
Dominik Locher
dominik.locher at bondsearch.ch
Sun Feb 24 14:04:58 CET 2008
Hi
I have some problems in defining new generic functions and classes. Just
have a look at the following example:
require(fPortfolio)
setClass("PROBECLASS",
representation(
type="character"
)
)
isGeneric("setType<-")
#Returns
TRUE
#I would like to define a specific function for class PROBECLASS with other
arguments than for the generic function "setType" of fPortfolio.
setGeneric("setType<-", function(object, value)
standardGeneric("setType<-"))
#Returns
Fehler in makeGeneric(name, fdef, fdeflt, group = group, valueClass =
valueClass, :
the formal arguments of the generic function for "setType<-" (object,
value) differ from those of the non-generic to be used as the default (spec,
value)
setReplaceMethod("setType", "PROBECLASS", function(object, value){
object at type <- value
object
})
#Example
obj = new("PROBECLASS")
setType(obj) = "test"
obj
######
If I don't require fPortfolio it works fine. However, is it not possible to
create two generic functions with the same name but different arguments?
setType for fPortfolio may be differ completely from setType of
PROBECLASS...
What's the best way to have functions which belongs to an object of a
specific class? I had a look at the paper "S4 Classes in 15 pages, more or
less" (feb12, 2003), however, I could not found what I did wrong...
Any help is highly appreciated.
Thanks
Dominik
More information about the R-help
mailing list