(1) It seems to me that, generally, in R it is not possible to overload functions. Is that right? (2) Assuming that the above is true, or partially true, is there any extra packages to handle overloading in R? (3) Assuming (1) is TRUE and (2) is FALSE, can anyone provide some advice on developing some function that understand what the arguments are and then calls the right overloaded function? It would be something like this: overloadedFunction1 <- function(x) {}; overloadedFunction2 <- function(x, y) {}; theFunction <- function(...) { # How to identify ... and call the right overloaded function? }
On Apr 20, 2005, at 8:16 AM, Ali - wrote:> (1) It seems to me that, generally, in R it is not possible to > overload functions. Is that right? > > (2) Assuming that the above is true, or partially true, is there any > extra packages to handle overloading in R? > > (3) Assuming (1) is TRUE and (2) is FALSE, can anyone provide some > advice on developing some function that understand what the arguments > are and then calls the right overloaded function? > > It would be something like this: > > overloadedFunction1 <- function(x) {}; > > overloadedFunction2 <- function(x, y) {}; > > theFunction <- function(...) > { > # How to identify ... and call the right overloaded function? > } >Ali, You are probably interested in "methods". Functions can have different "methods" depending on what the arguments and their types are. A first place to look is: http://cran.r-project.org/doc/manuals/R-exts.html#Generic-functions- and-methods Sean
If I understand what you are looking for, I believe your assumption (1) is FALSE, not TRUE as you suspected. One thing to realize is that "methods" in R are very different to those in other common "object-oriented" languages such as C++, Java, or Python. One difference is that methods are not intimately associated with a class definition -- the method dispatch system pulls disparate pieces of information together. Another difference is that the choice of which method to invoke can depend on all arguments, not just the first. Yet another thing to realize is that R has two class systems (commonly known as something like "S3" and "S4" methods and classes). Each has its own way of defining classes, generic functions and methods. The S4 class system offers more control over which method is called. Consult the "Green Book" for details of the S4 system. (See the FAQ for full details of the "Green Book"). Here's an example of something like what you are asking for, using the S4 class system. Note that this example doesn't involve the definition of any classes, just a generic and methods for some standard classes. Also note that a generic and its methods should have the same argument names. > setGeneric("foo", function(x,y) standardGeneric("foo")) [1] "foo" > setMethod("foo", c("ANY", "missing"), + function(x,y) "x:any y:missing") [1] "foo" > setMethod("foo", c("numeric", "numeric"), + function(x,y) "x:numeric y:numeric") [1] "foo" > setMethod("foo", c("missing", "numeric"), + function(x,y) "x:missing y:numeric") [1] "foo" > foo(1,2) [1] "x:numeric y:numeric" > foo(1) [1] "x:any y:missing" > foo(y=2) [1] "x:missing y:numeric" > foo(y="bar") Error in foo(y = "bar") : no direct or inherited method for function 'foo' for this call > (You can execute the above commands under Windows by copying the entire transcript to the clipboard, and then using the "Paste commands only" menu item under the "Edit" menu in the "R console" window.) hope this helps to at least get you started, Tony Plate Ali - wrote:> (1) It seems to me that, generally, in R it is not possible to overload > functions. Is that right? > > (2) Assuming that the above is true, or partially true, is there any > extra packages to handle overloading in R? > > (3) Assuming (1) is TRUE and (2) is FALSE, can anyone provide some > advice on developing some function that understand what the arguments > are and then calls the right overloaded function? > > It would be something like this: > > overloadedFunction1 <- function(x) {}; > > overloadedFunction2 <- function(x, y) {}; > > theFunction <- function(...) > { > # How to identify ... and call the right overloaded function? > } > > ______________________________________________ > R-devel@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >
On 4/20/05, Ali - <saveez@hotmail.com> wrote:> (1) It seems to me that, generally, in R it is not possible to overload > functions. Is that right? > > (2) Assuming that the above is true, or partially true, is there any extra > packages to handle overloading in R? > > (3) Assuming (1) is TRUE and (2) is FALSE, can anyone provide some advice on > developing some function that understand what the arguments are and then > calls the right overloaded function? > > It would be something like this: > > overloadedFunction1 <- function(x) {}; > > overloadedFunction2 <- function(x, y) {}; > > theFunction <- function(...) > { > # How to identify ... and call the right overloaded function? > }Here is an example using S3:> > f <- function(x, y) UseMethod("f") > > f.default <- function(x,y,z) {+ if (missing(z)) { + class.x <- if (missing(x)) "missing" else class(x) + class.y <- if (missing(y)) "missing" else class(y) + .Class <- paste(class.x, class.y, sep = ".") + NextMethod("f", z = 1) + } else # real default method + if (!missing(x) && !missing(y)) paste(x,y) else "one missing" + }> > f.missing.missing <- function(x, y, z) "both Missing" > f.numeric.numeric <- function(x,y, z) paste(x, y) > > f()[1] "both Missing"> f(1)[1] "one missing"> f(y=1)[1] "one missing"> f(1,1)[1] "1 1"
Hello,> -----Original Message----- > From: r-devel-bounces@stat.math.ethz.ch > [mailto:r-devel-bounces@stat.math.ethz.ch] On Behalf Of A.J. Rossini > Sent: Wednesday, April 20, 2005 12:21 PM > To: Ali - > Cc: r-devel@stat.math.ethz.ch > Subject: Re: [Rd] Overloading methods in R > > R.oo tries to implement an old-fashioned OO system as found > in Java, Python, C++, etc. R's S4 methods implement a nice > modern system based on the generic function approach , > dispatch on argument signatures,With all respect to R (and its developers) and for the records I couldn't help but refer to Ada'83 (not even '95 :-)) Best regards, Latchezar Dimitrov> which is different. > > While the R documentation for S4 classes is quite useful > (spanning the green book, the BioC developer help pages, > V&R's book on programming, and some other papers), I've found > that for a nice background, Paul Graham's ANSI Lisp book, and > in particular the nicely written chapter on CLOS, provides a > nice introduction to the thought process. > > With respect to the R.oo package, the author might be the > best source for that. > > Another package which you might take a look at is the proto > package, which provides prototype object-orientation similar > to that found in XLispStat, and also might help with what you > are trying to do. > However, I suspect that learning about the S4 system will > provide more benefit in the future. > > best, > -tony > > On 4/20/05, Ali - <saveez@hotmail.com> wrote: > > Sean, > > > > Thanks, but, I am actually talking about overloading > 'methods' and not > > 'functions', or you may want to answer this question: How > to overload > > methods in classes created by R.oo package? > > > > > > > >On Apr 20, 2005, at 8:16 AM, Ali - wrote: > > > > > >>(1) It seems to me that, generally, in R it is not possible to > > >>overload functions. Is that right? > > >> > > >>(2) Assuming that the above is true, or partially true, > is there any > > >>extra packages to handle overloading in R? > > >> > > >>(3) Assuming (1) is TRUE and (2) is FALSE, can anyone > provide some > > >>advice on developing some function that understand what the > > >>arguments are and then calls the right overloaded function? > > >> > > >>It would be something like this: > > >> > > >>overloadedFunction1 <- function(x) {}; > > >> > > >>overloadedFunction2 <- function(x, y) {}; > > >> > > >>theFunction <- function(...) > > >>{ > > >> # How to identify ... and call the right overloaded function? > > >>} > > >> > > > > > >Ali, > > > > > >You are probably interested in "methods". Functions can have > > >different "methods" depending on what the arguments and > their types > > >are. A first place to look is: > > > > > > >http://cran.r-project.org/doc/manuals/R-exts.html#Generic-functions- > > >and-methods > > > > > >Sean > > > > > > > ______________________________________________ > > R-devel@stat.math.ethz.ch mailing list > > https://stat.ethz.ch/mailman/listinfo/r-devel > > > > > -- > best, > -tony > > "Commit early,commit often, and commit in a repository from > which we can easily roll-back your mistakes" (AJR, 4Jan05). > > A.J. Rossini > blindglobe@gmail.com > > ______________________________________________ > R-devel@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >
>From: "A.J. Rossini" <blindglobe@gmail.com> > >You are mixing S3 and S4 paradigms. You want setMethod to define the >method, not the FunctionName.ClassOfObject S3 specification.That just proves my newbie-ness in R programming. I would appreciate it if you could provide me the S4 equivalent. According to the documentation in the methods package: "Computations using the notion of 'NULL' class attributes or of class attributes with multiple class names are not really compatible with the ideas in the 'methods' package." So I guess a different approach should be taken in S4?>On 4/22/05, Ali - <saveez@hotmail.com> wrote: > > > > > > > > > > >f <- function(...) UseMethod("f", NULL) > > > > > >f.NULL <- function(...) { > > > args <- list(...) > > > classes <- sapply(args, class) > > > .Class <- paste(classes, collapse = ".") > > > NextMethod("f", ...) > > >} > > > > > >f.numeric <- function(...) 2 * ..1 > > >f.numeric.numeric <- function(...) ..1 + ..2 > > >f.character.numeric.Date <- function(...) { > > > args <- list(...) > > > paste(args[[1]], args[[2]], format(args[[3]], "%Y-%m-%d")) > > >} > > >f.default <- function(...) print(list(...)) > > > > > > > > >f(1) # 2 > > >f(1,2) # 3 > > >f("a", 23, Sys.Date()) # "a 23 2005-04-21" > > >f() # list() > > > > Thanks Gabor! This answers a big part of my question. I am just curious >why > > something like this doesn't work in S4: > > > > ------------------------- > > setGeneric("foo", function(object, ...) standardGeneric("foo")) > > > > foo.NULL <- function(object, ...) { > > args <- list(...) > > classes <- sapply(args, class) > > .Class <- paste(classes, collapse = ".") > > } > > > > foo.default <- function(object, ...) paste("wrong args!") > > > > foo.numeric <- function(object, ...) 2 * ..1 > > foo.numeric.numeric <- function(object, ...) ..1 + ..2 > > > > ______________________________________________ > > R-devel@stat.math.ethz.ch mailing list > > https://stat.ethz.ch/mailman/listinfo/r-devel > > > > >-- >best, >-tony > >"Commit early,commit often, and commit in a repository from which we can >easily >roll-back your mistakes" (AJR, 4Jan05). > >A.J. Rossini >blindglobe@gmail.com