Wednesday, December 2, 2009

R difficulties

A project implementing individual-based simulations in R has been starting to stink a little. I did some design work to get the basic things that happen in an ecological simulation under control. How time moves forward, when the simulation state is recorded, and what kinds of data are recorded is pretty well defined via an S4 class and an associated method definition for the 'simulate' function (which already happened to have a generic).

My current problem is that the S4 class references a bunch of environments where data is kept in order to avoid copying large objects whenever the simulation iterates. The object also holds a list of functions, and a call order. The functions are called in order to modify data held in environments referenced by the object, and then time is iterated before doing it all over again. The enforcement of good behavior by the functions is pretty minimal and will come back to bite me once other people start writing functions using this setup.

The simplest answer I can come up with is to make an S4 class wrapper class for the functions this object will use. The wrapper class will have a slot for the function which operates on the object, and it will have two additional slots: one which states which parts of the object will be required, and another which states which parts of the object will be modified. Sort of like promises and requirements. It'll then require a method which checks for the requirements in the object first, and runs the function, and checks the object was only modified where stated...

No comments:

Post a Comment