[R] eval parent.frame() twice
Rune Schjellerup Philosof
rphilosof at health.sdu.dk
Fri Aug 7 13:36:35 CEST 2009
Peter Dalgaard skrev:
> Rune Schjellerup Philosof wrote:
>
>> Hi
>>
>> I want to use a function (update) that in its body uses
>> eval(call, parent.frame())
>>
>> I would like to use this function in a function that does not contain
>> the variables referred to in 'call'. Those variables are instead in the
>> parent.frame() of my function (named 'second' below)
>>
>> Like this:
>> a <- 2
>>
>> evalu <- function(obj) {
>> call <- obj$call
>> eval(call, parent.frame())
>> }
>> first <- function() {
>> a <- 3
>> obj <- list(call=expression(a))
>> second(obj)
>> }
>> second <- function(obj) {
>> eval(evalu(obj), parent.frame())
>> }
>>
>> first() #returns 2, but I want 3.
>>
>>
>> How do I change 'second' such that the value of 'a' from 'first' is
>> returned?
>>
>>
>
> You could use the "n" argument to parent.frame (or eval.parent) inside
> "evalu" and go two generations back instead of one. Somewhat neater, you
> could pass the desired environment directly to evalu, as in
>
> e <- parent.frame()
> evalu(obj, e)
>
> (I'm not sure it is actually needed to separate out the calculation of
> "e", but I tend to be paranoid about evaluating parent.frame() in
> function arguments.)
>
>
>
Thanks for the answer, but it isn't what I need.
'evalu' is a replacement for a function that I cannot change
(update.default).
My question is: How do I accomplish this by only changing 'second'?
--
Med venlig hilsen
Rune Schjellerup Philosof
Ph.d.-studerende, Statistik, IST, SDU
Telefon: 6550 3607
E-mail: rphilosof at health.sdu.dk
Adresse: J.B. Winsløwsvej 9, 5000 Odense C
More information about the R-help
mailing list