Thanks a lot for your explanation, which I higly appreciate. But I have
still a problem...
Think about 2 persons, each of them create their own package. Both define a
generic function "setType" with different arguments.
Person 1: setType(obj, valX)
Person 2: setType(spec)
If I require the package of person 1, everything works fine.
If I call the second package afterwards, I will get an error, because the
generic function already exists.
How can I solve this conflict? How can I define a new generic function which
has different arguments without getting in trouble with the first package?
Is there a way to define functions, which belongs to a specific class
without getting in troubles with other packages?
Thanks a lot for your help.
Dominik
-----Urspr?ngliche Nachricht-----
Von: Martin Morgan [mailto:mtmorgan at fhcrc.org]
Gesendet: Montag, 25. Februar 2008 19:54
An: Dominik Locher
Betreff: Re: AW: [R] Generic Functions
Things are different in R. You can't protect a new function from hiding your
function, just as
> print(1:10)
[1] 1 2 3 4 5 6 7 8 9 10> print <- function(x) "oops"
> print(1:10)
[1] "oops"> rm(print)
> print(1:10)
[1] 1 2 3 4 5 6 7 8 9 10
Note that the redefinition hides but does not remove 'print' function.
Generics don't really belong with classes, but if you think about it
something like
class Foo {
function bar() {}
}
foo = new Foo;
$foo->bar()
in php is very similar to
> setClass("Foo", "list")
> setGeneric("bar", function(x) standardGeneric("bar"))
setMethod("bar",
> "Foo", function(x) {}) foo = new("Foo")
> bar(foo)
i.e., set a method on the generic function. Someone could write another
method 'bar' operating on a different object, and it would coexist with
your
method.
Martin
"Dominik Locher" <dominik.locher at bondsearch.ch> writes:
> Hi
>
> Many thanks for your explanation. Just another short question. How can
> I make sure that if I greate a new class with functions, that nobody
> can change this functions anymore or as you mentioned overwrite
> unintended this function (setType).
>
> In OOP i.e. in php I have a class and specific functions belongs to
> this class. How can I do the same in R with generic functions? or is
> there another way?
>
> Thanks for your help.
>
> Dominik
>
>
> PS: I will send questions about S4 to R-devel at r-project.org in future.
;-).>
> -----Urspr?ngliche Nachricht-----
> Von: Martin Morgan [mailto:mtmorgan at fhcrc.org]
> Gesendet: Sonntag, 24. Februar 2008 17:59
> An: Dominik Locher
> Cc: r-help at r-project.org
> Betreff: Re: [R] Generic Functions
>
> See the 'useAsDefault' argument to setGeneric.
>
> As an aside, if 'setType<-' is meant to be a 'setter' to
change the
> value of a slot 'type', then I find the syntax a little redundant
--
> it's use
>
> > setType(x) <- "foo"
>
> implies that it is already a 'setter' without 'set' at the
front. Why
> not just
>
> > type(x) <- "foo"
>
> (though perhaps 'type' is not such a good name, either)?
>
> As a second aside, if you're writing code that you expect to be used
> with fPortfolio, then having two functions with the same name but
> different signatures or overall goals will confuse your user -- with
> fPortfolio,
> setType<- works fine, but then for mysterious reasons (i.e., when your
> package is loaded, with a different definition of setType<-) code that
> worked before no longer works! So I'd either use setType in a way
> consistent with it's use in fPortfolio, or define a new generic for
> your own purposes
> (setType<- is not a generic in my version of fPortfolio,
>
> > packageDescription('fPortfolio')$Version
> [1] "260.72"
>
> ).
>
> As a third aside, I think questions about S4 probably belong on
> R-devel, as they seem to fall in the realm of 'questions likely to
> prompt discussion unintelligible to non-programmers' (from the R-devel
> mailing list description).
>
> Martin
>
> Dominik Locher wrote:
>> 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
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
--
Martin Morgan
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109
Location: Arnold Building M2 B169
Phone: (206) 667-2793