Skip to content

Object orientation?

8 messages · Martyn Plummer, Duncan Murdoch, Peter Dalgaard +3 more

#
Hi,

I wonder how one could implement the following idea in R if it's possible of
course. Say you have an object, lets call it MyO. I would like MyO to
provides both properties and methods, encapsulation in OOP terms I think. I
would like to access MyO properties (only) via functions like {get, let,
set}.

For example:

	MyO.get(PropertyName="default", [Index]); where Index defaults to current
item
	MyO.let(PropertyName="default", Value)
	MyO.set(PropertyName="default", Value, [Index]); where Index defaults to a
new item

Up to now there is no real problem as I could use the following construct
for these let and set methods or functions: MyO <-
MyO.let(PropertyName="ThisProperty", ThisValue).

But what I would like to be able to do in order to let or set (as above) one
of MyO properties for example is only use:
MyO.let(PropertyName="ThisProperty", ThisValue).

So my question is: Can this be done in a proper and acceptable R fashion?

I know I could use super assign (<<-) but I think some say it's best
avoided.

Thanks for your time.

Yves Gauvreau
B.E.F.P. Universit? du Qu?bec ? Montr?al
cyg at sympatico.ca

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Have you looked at the scoping demo in source directory demos/language?
I think this will show you how to do it.
Martyn
On 24-Jan-01 Yves Gauvreau wrote:
current
to a
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Hi

I must offer my most sincere congratulations for the excellent work of the
folks that produce the new R-News.

Felicitation.

Yves Gauvreau
B.E.F.P. Universite du Quebec a Montreal
cyg at sympatico.ca

P.S. GSVIEW 2.9 on my NT system gave an error attempting to display page 8.
Could be a glitch caused be downloading or ...

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
I expect you could do something like

 let(MyO, ThisProperty = ThisValue)

with a declaration for the function like this:

  let <- function(obj, ...)
  {
     properties <- list(...)
     assign( something, envir = something)
  }

and playing around with the sys.frame function, substitute, etc., to
fill in the gaps. I think the details are in the R extensions manual.
Look at 

 get("mode<-")

for an example of a function call constructed from the arguments, and
an assignment made in a parent frame.

Duncan Murdoch
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
"Yves Gauvreau" <cyg at sympatico.ca> writes:
Generally, that's when people try to turn R (or S) into a macro
language. There could be situations where it's the right thing...

However, for this situation, how about something like

class(MyO) <- "foo"
"$<-.foo" <- function(x, name, value)
{
   if ( !is.null(x$let) )
	x$let(PropertyName=name, value)
   else
	x[[name]] <- value
}

MyO$ThisProperty <- ThisValue

(but wouldn't x$let or MyO.let need to have some way of figuring out
which object is its owner??)
#
On Wed, 24 Jan 2001, Yves Gauvreau wrote:
This is one of the acceptable uses of <<-, as shown in
demo(scoping).  <<- is useful for modifying a variable that you know
exists in the enclosing environment. Most of the problems come from people
trying to use it to modify things in a parent environment or in the global
environment.

	-thomas

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Could you be so kind as to explain what you mean by "turn R (or S) into a
macro language"
Maybe the way I expressed the idea wasn't clear enough or the way I
expressed it wasn't, so I'll try again. I have a personal project where I
would like to use R for it's statistical and graphical capabilities as is
usually the case presumably.

The strategy I thought up would probably be better handled (or easier to
implement) with an object orientation kind of approach. I was able to
identify many objects, in the since of OOP terminology, which would also
need to be handle as a group or as a collection (or as some kind of a
list|data.frame in R terms). I would also require a few of these groups to
be imbedded in higher level object and so on. My first idea was to imbed R
in a program written in a language like C++ for example. Before I start
working in this manner, I thought of investigating the possibilities of
doing all of this in R instead. So I bought "S Programming" from Venables &
Ripley and read it along with other material. So in the process I kind of
ask myself the same thing as you ask me above and I've got no answer. Maybe
I should of ask what are the facilities in R to write or implement an
hierarchy of object (classes) working together as in C++ or as close as
possible to that?

Thanks

Yves

PS I take this occasion to thank all other who tried to help as well.
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
2 days later
#
"Yves Gauvreau" <cyg at sympatico.ca> writes:
No, I can report occasional errors and partial page displays on gv
too, built on Ghostscript 5.10.