[Rd] Recommended style with calculator and persistent data

Ross Boylan ross at biostat.ucsf.edu
Thu May 18 20:23:40 CEST 2006

I have some calculations that require persistent state.  For example,
they retain most of the data across calls with different parameters.
They retain parameters across calls with different subsets of the cases
(this is for distributed computation).  They retain early analysis of
the problem to speed later computations.

I've created an S4 object, and the stylized code looks like this
calc <- makeCalculator(a, b, c)
calc <- setParams(calc, d, e)
calc <- compute(calc)
results <- results(calc)
The client code (such as that above) must remember to do the
assignments, not just invoke the functions.

I notice this does not seem to be the usual style, which is more like
results <- compute(calc)

and possibly using assignment operators like
params(calc) <- x
(actually, I have a call like that, but some of the updates take
multiple arguments).

Another route would be to use lexical scoping to bundle all the
functions together (umm, I'm not sure how that would work with S4
methods) to ensure persistence without requiring assignment by the
client code.  Obviously this would decrease portability to S, but I'm
already using lexical scope a bit.

Is there a recommended R'ish way to approach the problem?  My current
approach works, but I'm concerned it is non-standard, and so would be
unnatural for users.
Ross Boylan                                      wk:  (415) 514-8146
185 Berry St #5700                               ross at biostat.ucsf.edu
Dept of Epidemiology and Biostatistics           fax: (415) 514-8150
University of California, San Francisco
San Francisco, CA 94107-1739                     hm:  (415) 550-1062

More information about the R-devel mailing list