[R] Extending data.frame

Ken Williams ken.williams at thomson.com
Mon Dec 17 16:56:40 CET 2007


Hi,

I've got a long-running project whose data fits nicely into data.frame
objects in R.  As I accumulate more and more functions, I decided to switch
to an OO approach so I can organize things better.

Looking around at the various approaches to OO R, I came across R.oo, which
seems nice.

Where I'm currently stuck is getting my objects to be mutable.  For example,
in the following toy code, the addStuff() method has no effect:

> library(R.oo)
R.oo v1.3.0 (2006-08-29) successfully loaded. See ?R.oo for help.
> setConstructorS3("Foo", function(...) {
+   frame <- data.frame(foo=4, bar=3:5)
+   extend(frame, "Foo")
+ })
> setMethodS3("addStuff", "Foo", function(this, ...) { this$field <- 5 })
> f <- Foo(); f
  foo bar
1   4   3
2   4   4
3   4   5
> addStuff(f); f
  foo bar
1   4   3
2   4   4
3   4   5


Can anyone offer any advice?  I'm open to using a different OO system if
that's deemed advisable, I'm not very familiar with any of them.

Note that in my real (non-toy) application, I'll need arbitrary methods to
be able to read & write data to the object, so simple getField() and
setField() accessors won't be sufficient.

Thanks.


-- 
Ken Williams
Research Scientist
The Thomson Corporation
Eagan, MN



More information about the R-help mailing list