A primitive OO in R -- where next?
On 12-May-10 22:24:21, Erik Iverson wrote:
(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
Well, that's a neat trick! (It seems to depend on my having put the function name first in the list, though, which wasn't done with any specific intent). In fact, the sort of thing I have in mind as possible applications for the idea in that simple example, is that one could encapsulate a "Study" in a list, which would contain as components the various datasets available (quite possibly of different structures etc.), and the various functions which one might use to analyse different datasets, or to combine analyses, etc.; and possibly also a top-level "supervisor" function which could ensure "social behaviour" in that mob of entities. Pretty open-ended, really. Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 12-May-10 Time: 23:46:34 ------------------------------ XFMail ------------------------------