Thanks again for your so far on proto. I have another question.

What is the best way to "do stuff" based on data prior to calling a
function? I tried the code below without expr (and including commas after
data member assignments), but it errors out. I'd like to make decisions
based on inputs during proto object construction and prep my data_members
for use in the functions that (would) follow. I think I could probably get
around this with a small function with other functions within the same
proto object, but I'd rather not repeat that in each function...if that
makes sense. See my last line of code below:

makeProto = proto( expr={
  data_member1=NULL
  data_member2=5
  data_member3=NULL
  if(!is.null(data_member1)){
    with(.,data_member3 = data_member1 + data_member2)
  }
})
oo = makeProto$proto()
oo$data_member1 # NULL
oo$data_member2 # 5
oo$data_member3 # NULL
oo2 = makeProto$proto(data_member1 = 7)
oo2$data_member1 # 7
oo2$data_member2 # 5
oo2$data_member3 # I want this to be 12 (12 = 7 + 5), but I get NULL

Its late for me so hopefully this makes sense...

Thanks!

ben

On Fri, Feb 17, 2012 at 11:38 PM, Gabor Grothendieck <
ggrothendieck@gmail.com> wrote:

> On Sat, Feb 18, 2012 at 12:44 AM, Ben quant <ccquant@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
>

	[[alternative HTML version deleted]]

