From sfalcon at fhcrc.org  Thu May 18 21:40:20 2006
From: sfalcon at fhcrc.org (Seth Falcon)
Date: Thu, 18 May 2006 12:40:20 -0700
Subject: [Rd] Recommended style with calculator and persistent data
In-Reply-To: <1147976620.10544.43.camel@iron.psg.net> (Ross Boylan's message
	of "Thu, 18 May 2006 11:23:40 -0700")
References: <1147976620.10544.43.camel@iron.psg.net>
Message-ID: <m2r72r86l7.fsf@ziti.fhcrc.org>

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



[Rd] Recommended style with calculator and persistent data
Seth Falcon