On Sat, Feb 18, 2012 at 12:44 AM, Ben quant <ccquant at gmail.com>
wrote:> The code below works as expected but:
> Using the proto package, is this the best way to 1) make a parameter
> persist if the parameter is passed
> in with a value, 2) allow for calling the bias() function without a
> parameter assignment, 3) have
> the x2 value initialize as 5? Thanks for your feedback. Giving the
> proto package a test beat and
> establishing some templates for myself.
>
>> oo <- proto(expr = {x = c(10, 20, 15, 19, 17) ? ? ? ? ? ? ? ? ? ? x2
= 5 # so x2 initializes as 5, but can be overwritten with param assignment ? ? ?
? ? ? ? ? ? ? bias <- function(.,x2=.$x2) { # x2=.$x2 so no default param is
needed ? ? ? ? ? ? ? ? ? ? ? ? .$x2 = x2 # so x2 persists in the env ? ? ? ? ? ?
? ? ? ? ? ? .$x <- .$x + x2 ? ? ? ? ? ? ? ? ? ? } })> o = oo$proto()>
o$x # [1] 10 20 15 19 17> o$x2 #[1] 5> o$bias(x2 = 100)> o$x2 # [1]
100> o$x # [1] 110 120 115 119 117
>
This is not very different from what you have already but here it is
for comparison. Note that the with(...) line has the same meaning as
.$x <- .$x + .$x2 :
oo <- proto(
x = c(10, 20, 15, 19, 17),
x2 = 5,
bias = function(., x2) {
if (!missing(x2)) .$x2 <- x2
with(., x <- x + x2)
}
)
--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com