parent object programming question

Hi everyone,

I was wondering if someone knows a way to accomplish this task in R. I'm overriding the operators(+,-,*,/) in my own class which is similar to a data.frame. When you do something like this:

mc[3] <- mc[1] + mc[2]

I'd like the parent object mc to save the expression on the right side but not really execute the addition. Now I have the overloading working correctly but where would the next part happen...would it be part of my classes extract or is there something like an assignment operator I need to overload? In this other area will I have access to the parent object mc or will it only have the extracted mc[3]?

Appreciate any thoughts!


Cookie Scientist
Although you have overloaded the + operator for whatever class mc is, it appears that's not actually the method you are calling in the line you gave. Instead you are calling the + method of whatever elements are contained in mc.
> thing1 <- 1:5
> thing2 <- 6:10
> class(thing1) <- "thing"
> class(thing2) <- "thing"
> "+.thing" <- function(a,b){
+   parse(text=paste(a,"+",b))
+ }
> # addition of thing objects
> thing1 + thing2
expression(1 + 6, 2 + 7, 3 + 8, 4 + 9, 5 + 10)
> # addition of numerics
> thing1[5] + thing2[5]
[1] 15
> # addition of thing objects
> thingList <- list(thing1, thing2)
> thingList[[1]] + thingList[[2]]
expression(1 + 6, 2 + 7, 3 + 8, 4 + 9, 5 + 10)
I don't know all the details of what you're trying to accomplish, but one possible route would be to define the "[" method in such a way that is coerces elements to the appropriate class before returning them.
Actually it is the right call because my class is basically a data.frame so just like a data.frame variable df is of class data.frame so is df[1] or df["colname"].

Basically my class is a data less data.frame. It only contains the column names, types and possibly an expression. If you have a class and columns named "a" and "b" and wish to create a new column "c" you would do
>myclass["c"] <- myclass["a"] + myclass["b"]
This would add a column "c" to the class/data.frame but have an expression associated with it of a + b. Next you could do
>myclass["d"] <- myclass["c"] - myclass["b"]
This would add a column "d" to the class/data.frame but have an expression associated with it of a + b - b.

My "[" method currently gives a subset just like a data.frame would. So if myclass has columns "a" and "b" then myclass["a"] is just a class with column "a" in it which is the desired effect.
So what I don't understand is how I would take the command myclass["d"] <- myclass["c"] - myclass["b"] and after building the expression on the right side, assign it in the main parent class. I will try to go through the data.frame code and see how they accomplish this.
I see my error "[" routine is making a copy of the information in my class to produce the a new class that it returns. data.frame returns a data.frame but it is really just a part of the same one so if you change it, it also changes the original data.frame. @Jake thanks you did point me in the right direction of where the problem was.