Re: [R] Question about "evalq"

From: ronggui <ronggui.huang_at_gmail.com>
Date: Mon, 28 May 2007 12:40:30 +0800

That's great. I got it. Million thanks.

On 5/28/07, Gabor Grothendieck <ggrothendieck_at_gmail.com> wrote:
> On 5/27/07, ronggui <ronggui.huang@gmail.com> wrote:
> > Hi,Gabor Grothendieck, Thanks very much.
> >
> > On 5/27/07, Gabor Grothendieck <ggrothendieck_at_gmail.com> wrote:
> > > evalq looks like this:
> > >
> > > > evalq
> > > function (expr, envir, enclos)
> > > eval.parent(substitute(eval(quote(expr), envir, enclos)))
> > > <environment: namespace:base>
> > >
> > > so it seems the difference is that
> > >
> > > - eval(quote(), envir, enclos) evaluates envir and enclos
> > > in the current frame but
> > > - evalq evaluates them in the parent.frame.
> > >
> > > This may be easier to see in the following example:
> >
> > Yeah, This example make the question easier to understand.
> >
> > > x <- "G"
> > > f1 <- function() eval(quote(x), parent.frame())
> > > f2 <- function() evalq(x, parent.frame())
> > > f11 <- function() {
> > > x <- "a"
> > > f1()
> > > }
> > > f22 <- function() {
> > > x <- "b"
> > > f2()
> > > }
> > > f11() # a
> > > f22() # G
> > >
> > > To avoid this problem pass a variable whose value is
> > > to be enclos= rather than an expression to compute it:
> >
> > --This is a good idea.
> > --If "evalq evaluates them in the parent.frame", I expected that if I
> > change parent.frame(2) to parent.frame(1), I will get the answer.But I
> > can not actually. So what's wrong with my understanding?
> >
> > f1 <- function(x,digits=5) lapply(x, f2)
> > f2 <- function(x) {
> > evalq(print(digits), list(x=x), parent.frame(1))
> > }
> > f1(list(x1=1)) ##Error in print(digits) : object "digits" not found

>
>

> Good point. Insert a browser statement where the parent.frame
> call was and when it stops do a traceback. That will show you
> what the call stack looks like at that point in time.
>
>

> > f1 <- function(x,digits=5) lapply(x, f2)
> > f2 <- function(x) evalq(print(digits), list(x=x), { browser() } )
> > f1(list(x1=1)) ##Error in print(digits) : object "digits" not found
> Called from: eval(quote(print(digits)), list(x = x), {
> browser()
> })
> Browse[1]> traceback()
> 10: print(digits)
> 9: eval(expr, envir, enclos)
> 8: eval(quote(print(digits)), list(x = x), browser())
> 7: eval(expr, envir, enclos)
> 6: eval(expr, p)
> 5: eval.parent(substitute(eval(quote(expr), envir, enclos)))
> 4: evalq(print(digits), list(x = x), browser())
> 3: FUN(X[[1L]], ...)
> 2: lapply(x, f2)
> 1: f1(list(x1 = 1))
> Browse[1]>
>
-- 
Ronggui Huang
Department of Sociology
Fudan University, Shanghai, China

______________________________________________
R-help_at_stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Mon 28 May 2007 - 04:45:56 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 28 May 2007 - 05:31:34 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.