[Rd] Recommended style with calculator and persistent data

Seth Falcon sfalcon at fhcrc.org
Thu May 18 21:40:20 CEST 2006

Ross Boylan <ross at biostat.ucsf.edu> writes:

> 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).

I think the setReplaceMethod approach has some advantages in the sense
that you don't have to remember to capture the return.  You can handle
multiple parameters in a couple of ways:

params(calc, p="num_iter") <- 1000
params(calc, p="verbose") <- FALSE

params(calc) <- list(num_iter=100, verbose=FALSE)

  + seth

More information about the R-devel mailing list