Ross Boylan
2006-May-18 18:23 UTC
[Rd] Recommended style with calculator and persistent data
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
Seth Falcon
2006-May-18 19:40 UTC
[Rd] Recommended style with calculator and persistent data
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
Seemingly Similar Threads
- TI83 plus Texas Instrument Calculator, Serial cable and USB cable
- on.exit called on loading ?
- [Q] Get formal arguments of my implemented S4 method
- [Q] Get formal arguments of my implemented S4 method
- [LLVMdev] Proposal for GSoC project for improving llvm-test testsuite