[R] Extending data.frame

From: Ken Williams <ken.williams_at_thomson.com>
Date: Mon, 17 Dec 2007 09:56:40 -0600


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.


Ken Williams
Research Scientist
The Thomson Corporation
Eagan, MN

R-help_at_r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Mon 17 Dec 2007 - 16:13:26 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 17 Dec 2007 - 17:30:19 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.