Greetings All, Out of curiosity, I've just done a very primitive experiment: Obj <- list(Fun=sum, Dat=c(1,2,3,4)) Obj$Fun(Obj$Dat) # [1] 10 That sort of thing (much more sophisticated) must be documented mind-blowingly somewhere. Where? Where I stand right now: The above (and its immediately obvious generalisations, like Obj$Fun<-cos) is all I know about it so far. Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 12-May-10 Time: 22:48:14 ------------------------------ XFMail ------------------------------
Hi, On Wed, May 12, 2010 at 5:48 PM, Ted Harding <Ted.Harding at manchester.ac.uk> wrote:> Greetings All, > > Out of curiosity, I've just done a very primitive experiment: > > ?Obj <- list(Fun=sum, Dat=c(1,2,3,4)) > ?Obj$Fun(Obj$Dat) > ?# [1] 10 > > That sort of thing (much more sophisticated) must be documented > mind-blowingly somewhere. Where? > > Where I stand right now: The above (and its immediately obvious > generalisations, like Obj$Fun<-cos) is all I know about it so far.This doesn't really answer you question, but I believe the R.oo package uses this type mojo for its OO-style programming. Some documentation is here: http://www1.maths.lth.se/help/R/R.oo/ -- Steve Lianoglou Graduate Student: Computational Systems Biology | Memorial Sloan-Kettering Cancer Center | Weill Medical College of Cornell University Contact Info: http://cbio.mskcc.org/~lianos/contact
(Ted Harding) wrote:> Greetings All, > > Out of curiosity, I've just done a very primitive experiment: > > Obj <- list(Fun=sum, Dat=c(1,2,3,4)) > Obj$Fun(Obj$Dat) > # [1] 10 > > That sort of thing (much more sophisticated) must be documented > mind-blowingly somewhere. Where? > > Where I stand right now: The above (and its immediately obvious > generalisations, like Obj$Fun<-cos) is all I know about it so far.Well functions are just an object in R, so lists can of course contain them. My naive understanding is that you can think of function calls as simply lists where the first element is the function name, and the rest of the list are the arguments, so: > eval(as.call(Obj)) [1] 10
Ted.Harding-2 wrote:> > Greetings All, > > Out of curiosity, I've just done a very primitive experiment: > > Obj <- list(Fun=sum, Dat=c(1,2,3,4)) > Obj$Fun(Obj$Dat) > # [1] 10 > > That sort of thing (much more sophisticated) must be documented > mind-blowingly somewhere. Where? > > Where I stand right now: The above (and its immediately obvious > generalisations, like Obj$Fun<-cos) is all I know about it so far. > > Ted. >You may want to take a look at the Proto package- it allows you to create objects that provide a reference to "self" inside their member functions. Hope this helps! -Charlie ----- Charlie Sharpsteen Undergraduate-- Environmental Resources Engineering Humboldt State University -- View this message in context: http://r.789695.n4.nabble.com/A-primitive-OO-in-R-where-next-tp2196862p2196948.html Sent from the R help mailing list archive at Nabble.com.
There is a paper on the proto home page http://r-proto.googlecode.com -- click on External Links on the right of the home page and see Prototype-Based Programming in Statistical Computation. On Wed, May 12, 2010 at 5:48 PM, Ted Harding <Ted.Harding at manchester.ac.uk> wrote:> Greetings All, > > Out of curiosity, I've just done a very primitive experiment: > > ?Obj <- list(Fun=sum, Dat=c(1,2,3,4)) > ?Obj$Fun(Obj$Dat) > ?# [1] 10 > > That sort of thing (much more sophisticated) must be documented > mind-blowingly somewhere. Where? > > Where I stand right now: The above (and its immediately obvious > generalisations, like Obj$Fun<-cos) is all I know about it so far. > > Ted. > > -------------------------------------------------------------------- > E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> > Fax-to-email: +44 (0)870 094 0861 > Date: 12-May-10 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Time: 22:48:14 > ------------------------------ XFMail ------------------------------ > > ______________________________________________ > 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. >
R OO is documented for S3 classes under section 5 (Object-oriented programming) in the R language definition. I guess the issue is somewhat philosophial as to how you use it. R philosophy _mostly_ separates data from operations on data, so the OO model provides classes for data and essentially separate methods that apply to those classes. This is the kind of model sometimes called a 'visitor pattern'. An alternative is to include operations on the data within the data object, which sometimes has advantages if you want to simplify the look of code for things like display (instead of a display method for each class, one effectively sends a mesage to any object of the form "display yourself here"). In practice, of course, one ends up writing class-specific operations code; the difference is pretty much where it's stored. On balance there seems to me a rationale for a statistician to separate data from the operations formed on it; one collects and curates data carefuly, so it as a kind of lifecycle of its own that is unrelated to mathematical operations performed on it. But I have allowed _data_ objects to include functions or at least function names when it is a necessary part of the description of the data. For example, in some of our interlaboratory studies labs give uncertainty information in the form of a variance or interval, but may additionally tell us what the assumed distribution is (eg Normal, t, lognormal etc). It then makes sense to have the distribution as part of the data. For these functions, the root name (norm, t, etc)_ suffices in conjunction with do.call, but to generalise completely, one can consider allowing a user to specify the distribution as (say) some arbitrary density function or density/probability family. (It's pretty rare that we'd need that, but hey - thinking ahead and all that). That would generate data which in part consisted of a function describing the (assumed) associated distribution. Steve Ellison>>> Ted Harding <Ted.Harding at manchester.ac.uk> 12/05/2010 22:48:17 >>>Greetings All, Out of curiosity, I've just done a very primitive experiment: Obj <- list(Fun=sum, Dat=c(1,2,3,4)) Obj$Fun(Obj$Dat) # [1] 10 That sort of thing (much more sophisticated) must be documented mind-blowingly somewhere. Where? Where I stand right now: The above (and its immediately obvious generalisations, like Obj$Fun<-cos) is all I know about it so far. Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 12-May-10 Time: 22:48:14 ------------------------------ XFMail ------------------------------ ______________________________________________ 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. ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}
This is not really OO at all, in my opinion. It's an example of the amazing flexibility of the language. I'd like to add on to what Erik said, with an example:> sum(1:10)[1] 55> foo <- sum > foo(1:10)[1] 55> junk <- list(a=sum) > junk$a(1:10)[1] 55 sum is an R object; it happens to be a function. When I do foo <- sum I'm creating another R object. It's also a function, so I use it with the same syntax. When I do junk <- list(a=sum) I'm creating another R object. It's a list, and its first element, named 'a', is a function. Since the element is a function, I use it just like any other function, i.e, follow its name with a pair of parentheses with arguments between them. Note that in the last example it doesn't matter what the other elements in the list, if any, are. I could just as well do junk <- list( foo=data.frame(x=1:4), b=c('x','y'), dd=sum) Then junk$dd(1:10) junk$dd( junk$foo$x ) are valid statements. There's no connection between using 'junk' both inside the parentheses and outside. Since junk$dd is a function, you can supply it with any R object, and it doesn't matter where that R object comes from. I doubt that it's documented in the way you might be expecting. It's a result of the generality of list elements -- they can be any R object. Hope this helps. -Don At 10:48 PM +0100 5/12/10, Ted Harding wrote:>Greetings All, > >Out of curiosity, I've just done a very primitive experiment: > > Obj <- list(Fun=sum, Dat=c(1,2,3,4)) > Obj$Fun(Obj$Dat) > # [1] 10 > >That sort of thing (much more sophisticated) must be documented >mind-blowingly somewhere. Where? > >Where I stand right now: The above (and its immediately obvious >generalisations, like Obj$Fun<-cos) is all I know about it so far. > >Ted. > >-------------------------------------------------------------------- >E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> >Fax-to-email: +44 (0)870 094 0861 >Date: 12-May-10 Time: 22:48:14 >------------------------------ XFMail ------------------------------ > >______________________________________________ >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.-- -------------------------------------- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062