A primitive OO in R -- where next?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

A primitive OO in R -- where next?

Ted.Harding-2
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) <[hidden email]>
Fax-to-email: +44 (0)870 094 0861
Date: 12-May-10                                       Time: 22:48:14
------------------------------ XFMail ------------------------------

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: A primitive OO in R -- where next?

Steve Lianoglou-6
Hi,

On Wed, May 12, 2010 at 5:48 PM, Ted Harding
<[hidden email]> 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: A primitive OO in R -- where next?

Erik Iverson-3
In reply to this post by Ted.Harding-2


(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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: A primitive OO in R -- where next?

Ted.Harding-2
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) <[hidden email]>
Fax-to-email: +44 (0)870 094 0861
Date: 12-May-10                                       Time: 23:46:34
------------------------------ XFMail ------------------------------

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: A primitive OO in R -- where next?

Sharpie
In reply to this post by Ted.Harding-2
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
Reply | Threaded
Open this post in threaded view
|

Re: A primitive OO in R -- where next?

Gabor Grothendieck
In reply to this post by Ted.Harding-2
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
<[hidden email]> 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) <[hidden email]>
> Fax-to-email: +44 (0)870 094 0861
> Date: 12-May-10                                       Time: 22:48:14
> ------------------------------ XFMail ------------------------------
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: A primitive OO in R -- where next?

slre
In reply to this post by Ted.Harding-2
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 <[hidden email]> 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) <[hidden email]>
Fax-to-email: +44 (0)870 094 0861
Date: 12-May-10                                       Time: 22:48:14
------------------------------ XFMail ------------------------------

______________________________________________
[hidden email] 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}}

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: A primitive OO in R -- where next?

Liaw, Andy
In reply to this post by Ted.Harding-2
From: [hidden email]

>
> 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).

This has not much to do with OO in the usual sense, just how functions
in R work.  See:
http://stat.ethz.ch/R-manual/R-devel/doc/manual/R-lang.html#Function-obj
ects

The styles of OO in S3 and S4 do not encapsulate methods into the class
definitions.

Andy

> 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) <[hidden email]>
> Fax-to-email: +44 (0)870 094 0861
> Date: 12-May-10                                       Time: 23:46:34
> ------------------------------ XFMail ------------------------------
>
> ______________________________________________
> [hidden email] 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.
>
Notice:  This e-mail message, together with any attachme...{{dropped:11}}

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: A primitive OO in R -- where next?

Don MacQueen
In reply to this post by Ted.Harding-2
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) <[hidden email]>
>Fax-to-email: +44 (0)870 094 0861
>Date: 12-May-10                                       Time: 22:48:14
>------------------------------ XFMail ------------------------------
>
>______________________________________________
>[hidden email] 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: A primitive OO in R -- where next?

Muenchen, Robert A (Bob)
In reply to this post by slre
Hi All,

This was a very interesting question & I enjoyed reading everyone's
responses. I've played around with it and summarized some of the
variations below.

Cheers,
Bob

# A fun example of how a list can store both a function
# and data for that function.

# Create a list that contains both a function and some data:

myList <- list(Fun=mean, x=c(1,2,3,4,5))
myList

# Execute the function on the data
myList$Fun(myList$x)

# Alternately, put the data into the function call:

as.call(myList)

# And evaluate it:

eval( as.call(myList) )

# Why does this not work?

myList <- list(Fun=mean, mydata=c(1,2,3,4,5))
eval( as.call(myList) )

# Because "mean" has an "x" argument
# and that does not exist since it's now named "mydata"

myList <- list(Fun=mean, x=c(1,2,3,4,5))
eval( as.call(myList) )

# And how does order affect it?
# Let's put the data first

myList <- list(x=c(1,2,3,4,5), Fun=mean)

# This works fine:
myList$Fun(myList$x)

# But look at what as.call tries to do:
as.call(myList)

# So evaluating it would be nonsense:
eval( as.call(myList) )



>-----Original Message-----
>From: [hidden email]
[mailto:[hidden email]]

>On Behalf Of S Ellison
>Sent: Thursday, May 13, 2010 8:07 AM
>To: Ted Harding; [hidden email]
>Subject: Re: [R] A primitive OO in R -- where next?
>
>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 <[hidden email]> 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) <[hidden email]>
>Fax-to-email: +44 (0)870 094 0861
>Date: 12-May-10                                       Time: 22:48:14
>------------------------------ XFMail ------------------------------
>
>______________________________________________
>[hidden email] 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}}
>
>______________________________________________
>[hidden email] 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.

______________________________________________
[hidden email] 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.