[R] proto question
Gabor Grothendieck
ggrothendieck at gmail.com
Wed Jan 7 20:13:31 CET 2009
On Wed, Jan 7, 2009 at 10:22 AM, Simon Knapp
<S.Knapp at mmassociates.com.au> wrote:
> Dear R Users,
>
>
>
> I have a couple of proto objects like:
>
>
>
> wedge <- proto(expr={
>
> start.year <- 2008
>
> end.year <- 2050
>
> })
>
>
>
> star.rating <- wedge$proto(
>
> star = c(4, 5, 8, 10),
>
> gain = c(0, .3, .5, .7),
>
> cost = c(0, 2100, 4000, 7500),
>
>
>
> star.rating <- function(., year) 6.0,
>
>
>
> setup = function(.){
>
> .$cost.for.star <- approxfun(.$star, .$cost)
>
> .$gain.for.star <- approxfun(.$star, .$gain)
>
> },
>
>
>
> test = function(., year) {
>
> gs <- .$with(gain.for.star)(.$star.rating(year))
>
> }
>
> )
>
>
>
>
>
>
>
> And a function to create and modify 'instances':
>
>
>
> create.star.rating <- function(switch.years, star.ratings) {
>
> res <- star.rating$proto(switch.years = switch.years, star.ratings =
> star.ratings)
>
> res$setup()
>
> res
>
> }
>
>
>
>
>
> When I use them as follows:
>
> ee <- create.star.rating(ratings, switch.years)
Did you define switch.years somewhere? The order of
arguments in the call to create.star.rating seems
reversed from the definition. There may be other problems
too but suggest you start by fixing those.
>
> ee$test(2009)
>
>
>
>
>
> The second line gives me the error message:
>
> "Error in .$with(gain.for.star)(.$star.rating(year)) :
>
> object "y" not found"
>
>
>
> I understand why this happens (when objects are inserted into a proto
> their environment is set to that of the proto)... but I can't figure out
> how to get around it!
>
>
>
> I have tried setting the environments of the functions created in setup
> as follows:
>
>
>
> setup = function(.){
>
> t1 <- approxfun(.$star, .$cost)
>
> t2 <- approxfun(.$star, .$gain)
>
> .$cost.for.star <- t1
>
> .$gain.for.star <- t2
>
> environment(.$cost.for.star) <- environment(t1)
>
> environment(.$gain.for.star) <- environment(t2)
>
> }
>
>
>
>
>
> But then I get the error:
>
> "Error in get("gain.for.star", env = `*tmp*`, inherits = TRUE) :
>
> object "*tmp*" not found"
>
>
>
>
>
> Which I think occurs because the last two lines in 'setup' only create
> references to their respective environments which disappear on exit of
> 'setup'.
>
>
>
>
>
>
>
> Any ideas?
>
>
>
> Regards,
>
> Simon Knapp.
More information about the R-help
mailing list