I found a tutorial for creating classes using generic functions ? S3 way ! It was short description so I couldn't grok in full its usage ... So far so good, what i currently do is something like this : Blah <- function(data,...) UseMethod('Blah') Blah.default <- function( ...... ) { self = .... class(self) <- 'Blah' self } Blah.some_method <- function(self, .....) { self$abc ......and so on } My main "concerns" is now I have to call methods like this : Blah.some_method(obj,.....) isn't there some more shortcut syntax something along the lines of ;) : obj.some_method(...) Also if you can point me to some more documentation about using this 'generic function' design in R.>From what I saw there is also some R.oo class for building OOP likeinterface, but it seems even more cumbersome to use. Any link you can give me which discusses those concept in regards to R, with examples too ;) (Seems to me like a mix of javascript like prototyping with Perl bless class name labeling, like it much better than the rigid class oriented languages like Java ;) ) PS> One of the biggest hurdles with R is that the language name is one character long and when you try to find some docs or solutions google returns alot of garbage not related to R ;) Otherwise I like very much the environment and the capabilities of the package, just that I'm pretty new with all statistics terminology and help pages are made for ppl who understand statistics, but anyway it is very valuable tool for learning.
In S3 you pass the object as the first argument of the method (rather than prefixing the method with the object as is common in other languages). When you call a method you write method(obj, ...whatever...) # correct and it automatically inspect the class of obj and if its class is Blah then it dispatches the call to method.Blah for you. You don't explicitly refer to Blah in the call. That is: method.Blah(obj, ...whatever...) # wrong If its the fact that method precedes the object rather than the object preceding the method that's bothersome to you because you are familiar with other languages then that is just how it is in S3. The proto package does support a syntax in which the object prefaces the method:> library(proto) > p <- proto(a = 1, add = function(self, num) self$a <- self$a + 1) > p$add(3) > p$a[1] 2 On Wed, May 13, 2009 at 7:01 PM, myshare <mraptor at gmail.com> wrote:> I found a tutorial for creating classes using generic functions ? S3 > way ! It was short description so I couldn't grok in full its > usage ... So far so good, what i currently do is something like this : > > Blah <- function(data,...) ?UseMethod('Blah') > > Blah.default <- function( ...... ) { > ? self = .... > ? class(self) <- 'Blah' > ? self > > } > > Blah.some_method <- function(self, .....) { > ? ? self$abc ......and so on > > } > > My main "concerns" is now I have to call methods like this : > > Blah.some_method(obj,.....) > > isn't there some more shortcut syntax something along the lines > of ?;) : > > obj.some_method(...) > > Also if you can point me to some more documentation about using this > 'generic function' design in R. > >From what I saw there is also some R.oo class for building OOP like > interface, but it seems even more > cumbersome to use. > > Any link you can give me which discusses those concept in regards to > R, with examples too ;) > (Seems to me like a mix of javascript like prototyping with Perl bless > class name labeling, > like it much better than the rigid class oriented languages like Java ;) ) > > PS> > One of the biggest hurdles with R is that the language name is one > character long and when you try to find some > docs or solutions google returns alot of garbage not related to R ;) > Otherwise I like very much the environment and the capabilities of the > package, just that I'm pretty new with all statistics terminology and > help pages are made for ppl who > understand statistics, but anyway it is very valuable tool for learning. > > ______________________________________________ > 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. >
tried that does not seem to work, for me ? Always have to specify it fully : someclass.blah(x) On Wed, May 13, 2009 at 11:10 PM, Jeff Laake <Jeff.Laake at noaa.gov> wrote:> class(x)="someclass" > > then blah(x) > > will invoke blah.someclass > > Just look at all the example code in R. ?For example summary.glm for > summary(x) where x is of class glm. > > > myshare wrote: >> >> I found a tutorial for creating classes using generic functions ? S3 >> way ! It was short description so I couldn't grok in full its >> usage ... So far so good, what i currently do is something like this : >> >> Blah <- function(data,...) ?UseMethod('Blah') >> >> Blah.default <- function( ...... ) { >> ? self = .... >> ? class(self) <- 'Blah' >> ? self >> >> } >> >> Blah.some_method <- function(self, .....) { >> ? ? self$abc ......and so on >> >> } >> >> My main "concerns" is now I have to call methods like this : >> >> Blah.some_method(obj,.....) >> >> isn't there some more shortcut syntax something along the lines >> of ?;) : >> >> obj.some_method(...) >> >> Also if you can point me to some more documentation about using this >> 'generic function' design in R. >> >From what I saw there is also some R.oo class for building OOP like >> interface, but it seems even more >> cumbersome to use. >> >> Any link you can give me which discusses those concept in regards to >> R, with examples too ;) >> (Seems to me like a mix of javascript like prototyping with Perl bless >> class name labeling, >> like it much better than the rigid class oriented languages like Java ;) ) >> >> PS> >> One of the biggest hurdles with R is that the language name is one >> character long and when you try to find some >> docs or solutions google returns alot of garbage not related to R ;) >> Otherwise I like very much the environment and the capabilities of the >> package, just that I'm pretty new with all statistics terminology and >> help pages are made for ppl who >> understand statistics, but anyway it is very valuable tool for learning. >> >> ______________________________________________ >> 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. >> >