[R] Question about "evalq"

2007-05-26 Thread ronggui
The help page of eval says: The 'evalq' form is equivalent to
'eval(quote(expr), ...)'.  But the following is not equivalent.  Can
anyone give me some explaination? Thanks very much.

> f1 <- function(x,digits=5) lapply(x, f2)
> f2 <- function(x) 
> eval(quote(print(x+1,digits=digits)),list(x=x),parent.frame(2))
> f1(list(x1=1))
[1] 2
$x1
[1] 2

>
> f1 <- function(x,digits=5) lapply(x, f2)
> f2 <- function(x) evalq(print(x+1,digits=digits),list(x=x),parent.frame(2))
> f1(list(x1=1))
Error in print.default(x + 1, digits = digits) :
  object "digits" not found



-- 
Ronggui Huang
Department of Sociology
Fudan University, Shanghai, China

__
R-help@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.


Re: [R] Question about "evalq"

2007-05-26 Thread Gabor Grothendieck
evalq looks like this:

   > evalq
   function (expr, envir, enclos)
   eval.parent(substitute(eval(quote(expr), envir, enclos)))
   

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:

   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:

   f1 <- function(x,digits=5) lapply(x, f2)
   f2 <- function(x) {
  pf2 <- parent.frame(2)
  evalq(print(digits), list(x=x), pf2)
   }
   f1(list(x1=1)) # 5



On 5/26/07, ronggui <[EMAIL PROTECTED]> wrote:
> The help page of eval says: The 'evalq' form is equivalent to
> 'eval(quote(expr), ...)'.  But the following is not equivalent.  Can
> anyone give me some explaination? Thanks very much.
>
> > f1 <- function(x,digits=5) lapply(x, f2)
> > f2 <- function(x) 
> > eval(quote(print(x+1,digits=digits)),list(x=x),parent.frame(2))
> > f1(list(x1=1))
> [1] 2
> $x1
> [1] 2
>
> >
> > f1 <- function(x,digits=5) lapply(x, f2)
> > f2 <- function(x) evalq(print(x+1,digits=digits),list(x=x),parent.frame(2))
> > f1(list(x1=1))
> Error in print.default(x + 1, digits = digits) :
>  object "digits" not found
>
>
>
> --
> Ronggui Huang
> Department of Sociology
> Fudan University, Shanghai, China
>
> __
> R-help@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.
>

__
R-help@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.


Re: [R] Question about "evalq"

2007-05-26 Thread Prof Brian Ripley
The meaning of parent.frame depends on where it is evaluated.  So one 
should not expect it to do the same thing in two equivalent expressions 
(and nor should one expect deparse to do so, for example).

A pretty close analogy is that using a symbolic link in a file system is 
equivalent to using the original file path, at least until you try '..' or 
'pwd' on the path.  (In the case of 'pwd' it depends on the OS: POSIX 
only requires '_an_ absolute pathname'.)

On Sun, 27 May 2007, ronggui wrote:

> The help page of eval says: The 'evalq' form is equivalent to
> 'eval(quote(expr), ...)'.  But the following is not equivalent.  Can
> anyone give me some explaination? Thanks very much.
>
>> f1 <- function(x,digits=5) lapply(x, f2)
>> f2 <- function(x) 
>> eval(quote(print(x+1,digits=digits)),list(x=x),parent.frame(2))
>> f1(list(x1=1))
> [1] 2
> $x1
> [1] 2
>
>>
>> f1 <- function(x,digits=5) lapply(x, f2)
>> f2 <- function(x) evalq(print(x+1,digits=digits),list(x=x),parent.frame(2))
>> f1(list(x1=1))
> Error in print.default(x + 1, digits = digits) :
>  object "digits" not found

-- 
Brian D. Ripley,  [EMAIL PROTECTED]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595

__
R-help@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.


Re: [R] Question about "evalq"

2007-05-27 Thread ronggui
In that "the meaning of parent.frame depends on where it is
evaluated", is there a nice way to figure out which frame an express
is evaluated? for example, I would like to konw what does
parent.frame(2)  refer to.

> f1 <- function(x,digits=5) lapply(x, f2)
> f2 <- function(x) evalq(print(x+1,digits=digits),list(x=x),parent.frame(2))
> f1(list(x1=1))
Error in print.default(x + 1, digits = digits) :
 object "digits" not found


On 5/27/07, Prof Brian Ripley <[EMAIL PROTECTED]> wrote:
> The meaning of parent.frame depends on where it is evaluated.  So one
> should not expect it to do the same thing in two equivalent expressions
> (and nor should one expect deparse to do so, for example).
>
> A pretty close analogy is that using a symbolic link in a file system is
> equivalent to using the original file path, at least until you try '..' or
> 'pwd' on the path.  (In the case of 'pwd' it depends on the OS: POSIX
> only requires '_an_ absolute pathname'.)
>
> On Sun, 27 May 2007, ronggui wrote:
>
> > The help page of eval says: The 'evalq' form is equivalent to
> > 'eval(quote(expr), ...)'.  But the following is not equivalent.  Can
> > anyone give me some explaination? Thanks very much.
> >
> >> f1 <- function(x,digits=5) lapply(x, f2)
> >> f2 <- function(x) 
> >> eval(quote(print(x+1,digits=digits)),list(x=x),parent.frame(2))
> >> f1(list(x1=1))
> > [1] 2
> > $x1
> > [1] 2
> >
> >>
> >> f1 <- function(x,digits=5) lapply(x, f2)
> >> f2 <- function(x) evalq(print(x+1,digits=digits),list(x=x),parent.frame(2))
> >> f1(list(x1=1))
> > Error in print.default(x + 1, digits = digits) :
> >  object "digits" not found
>
> --
> Brian D. Ripley,  [EMAIL PROTECTED]
> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
> University of Oxford, Tel:  +44 1865 272861 (self)
> 1 South Parks Road, +44 1865 272866 (PA)
> Oxford OX1 3TG, UKFax:  +44 1865 272595
>


-- 
Ronggui Huang
Department of Sociology
Fudan University, Shanghai, China

__
R-help@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.


Re: [R] Question about "evalq"

2007-05-27 Thread ronggui
Hi,Gabor Grothendieck, Thanks very much.

On 5/27/07, Gabor Grothendieck <[EMAIL PROTECTED]> wrote:
> evalq looks like this:
>
>> evalq
>function (expr, envir, enclos)
>eval.parent(substitute(eval(quote(expr), envir, enclos)))
>
>
> 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


>f1 <- function(x,digits=5) lapply(x, f2)
>f2 <- function(x) {
>   pf2 <- parent.frame(2)
>   evalq(print(digits), list(x=x), pf2)
>}
>f1(list(x1=1)) # 5
>
>
>
> On 5/26/07, ronggui <[EMAIL PROTECTED]> wrote:
> > The help page of eval says: The 'evalq' form is equivalent to
> > 'eval(quote(expr), ...)'.  But the following is not equivalent.  Can
> > anyone give me some explaination? Thanks very much.
> >
> > > f1 <- function(x,digits=5) lapply(x, f2)
> > > f2 <- function(x) 
> > > eval(quote(print(x+1,digits=digits)),list(x=x),parent.frame(2))
> > > f1(list(x1=1))
> > [1] 2
> > $x1
> > [1] 2
> >
> > >
> > > f1 <- function(x,digits=5) lapply(x, f2)
> > > f2 <- function(x) 
> > > evalq(print(x+1,digits=digits),list(x=x),parent.frame(2))
> > > f1(list(x1=1))
> > Error in print.default(x + 1, digits = digits) :
> >  object "digits" not found
> >
> >
> >
> > --
> > Ronggui Huang
> > Department of Sociology
> > Fudan University, Shanghai, China
> >
> > __
> > R-help@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.
> >
>


-- 
Ronggui Huang
Department of Sociology
Fudan University, Shanghai, China

__
R-help@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.


Re: [R] Question about "evalq"

2007-05-27 Thread Gabor Grothendieck
On 5/27/07, ronggui <[EMAIL PROTECTED]> wrote:
> Hi,Gabor Grothendieck, Thanks very much.
>
> On 5/27/07, Gabor Grothendieck <[EMAIL PROTECTED]> wrote:
> > evalq looks like this:
> >
> >> evalq
> >function (expr, envir, enclos)
> >eval.parent(substitute(eval(quote(expr), envir, enclos)))
> >
> >
> > 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]>

__
R-help@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.


Re: [R] Question about "evalq"

2007-05-27 Thread ronggui
That's great. I got it. Million thanks.

On 5/28/07, Gabor Grothendieck <[EMAIL PROTECTED]> wrote:
> On 5/27/07, ronggui <[EMAIL PROTECTED]> wrote:
> > Hi,Gabor Grothendieck, Thanks very much.
> >
> > On 5/27/07, Gabor Grothendieck <[EMAIL PROTECTED]> wrote:
> > > evalq looks like this:
> > >
> > >> evalq
> > >function (expr, envir, enclos)
> > >eval.parent(substitute(eval(quote(expr), envir, enclos)))
> > >
> > >
> > > 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@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.


Re: [R] Question about "evalq"

2007-05-27 Thread Prof Brian Ripley
How about 'traceback'?  (It does not necesssarily show all the frames, but 
it does help and the exceptions are fairly esoteric.)

On Mon, 28 May 2007, ronggui wrote:

> In that "the meaning of parent.frame depends on where it is
> evaluated", is there a nice way to figure out which frame an express
> is evaluated? for example, I would like to konw what does
> parent.frame(2)  refer to.
>
>> f1 <- function(x,digits=5) lapply(x, f2)
>> f2 <- function(x) evalq(print(x+1,digits=digits),list(x=x),parent.frame(2))
>> f1(list(x1=1))
> Error in print.default(x + 1, digits = digits) :
> object "digits" not found
>
>
> On 5/27/07, Prof Brian Ripley <[EMAIL PROTECTED]> wrote:
>> The meaning of parent.frame depends on where it is evaluated.  So one
>> should not expect it to do the same thing in two equivalent expressions
>> (and nor should one expect deparse to do so, for example).
>> 
>> A pretty close analogy is that using a symbolic link in a file system is
>> equivalent to using the original file path, at least until you try '..' or
>> 'pwd' on the path.  (In the case of 'pwd' it depends on the OS: POSIX
>> only requires '_an_ absolute pathname'.)
>> 
>> On Sun, 27 May 2007, ronggui wrote:
>> 
>> > The help page of eval says: The 'evalq' form is equivalent to
>> > 'eval(quote(expr), ...)'.  But the following is not equivalent.  Can
>> > anyone give me some explaination? Thanks very much.
>> >
>> >> f1 <- function(x,digits=5) lapply(x, f2)
>> >> f2 <- function(x) 
>> eval(quote(print(x+1,digits=digits)),list(x=x),parent.frame(2))
>> >> f1(list(x1=1))
>> > [1] 2
>> > $x1
>> > [1] 2
>> >
>> >>
>> >> f1 <- function(x,digits=5) lapply(x, f2)
>> >> f2 <- function(x) 
>> evalq(print(x+1,digits=digits),list(x=x),parent.frame(2))
>> >> f1(list(x1=1))
>> > Error in print.default(x + 1, digits = digits) :
>> >  object "digits" not found
>> 
>> --
>> Brian D. Ripley,  [EMAIL PROTECTED]
>> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
>> University of Oxford, Tel:  +44 1865 272861 (self)
>> 1 South Parks Road, +44 1865 272866 (PA)
>> Oxford OX1 3TG, UKFax:  +44 1865 272595
>> 
>
>
>

-- 
Brian D. Ripley,  [EMAIL PROTECTED]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595

__
R-help@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.


Re: [R] Question about "evalq"

2007-05-27 Thread ronggui
Got it. Thanks very much.

On 5/28/07, Prof Brian Ripley <[EMAIL PROTECTED]> wrote:
> How about 'traceback'?  (It does not necesssarily show all the frames, but
> it does help and the exceptions are fairly esoteric.)
>
> On Mon, 28 May 2007, ronggui wrote:
>
> > In that "the meaning of parent.frame depends on where it is
> > evaluated", is there a nice way to figure out which frame an express
> > is evaluated? for example, I would like to konw what does
> > parent.frame(2)  refer to.
> >
> >> f1 <- function(x,digits=5) lapply(x, f2)
> >> f2 <- function(x) evalq(print(x+1,digits=digits),list(x=x),parent.frame(2))
> >> f1(list(x1=1))
> > Error in print.default(x + 1, digits = digits) :
> > object "digits" not found
> >
> >
> > On 5/27/07, Prof Brian Ripley <[EMAIL PROTECTED]> wrote:
> >> The meaning of parent.frame depends on where it is evaluated.  So one
> >> should not expect it to do the same thing in two equivalent expressions
> >> (and nor should one expect deparse to do so, for example).
> >>
> >> A pretty close analogy is that using a symbolic link in a file system is
> >> equivalent to using the original file path, at least until you try '..' or
> >> 'pwd' on the path.  (In the case of 'pwd' it depends on the OS: POSIX
> >> only requires '_an_ absolute pathname'.)
> >>
> >> On Sun, 27 May 2007, ronggui wrote:
> >>
> >> > The help page of eval says: The 'evalq' form is equivalent to
> >> > 'eval(quote(expr), ...)'.  But the following is not equivalent.  Can
> >> > anyone give me some explaination? Thanks very much.
> >> >
> >> >> f1 <- function(x,digits=5) lapply(x, f2)
> >> >> f2 <- function(x)
> >> eval(quote(print(x+1,digits=digits)),list(x=x),parent.frame(2))
> >> >> f1(list(x1=1))
> >> > [1] 2
> >> > $x1
> >> > [1] 2
> >> >
> >> >>
> >> >> f1 <- function(x,digits=5) lapply(x, f2)
> >> >> f2 <- function(x)
> >> evalq(print(x+1,digits=digits),list(x=x),parent.frame(2))
> >> >> f1(list(x1=1))
> >> > Error in print.default(x + 1, digits = digits) :
> >> >  object "digits" not found
> >>
> >> --
> >> Brian D. Ripley,  [EMAIL PROTECTED]
> >> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
> >> University of Oxford, Tel:  +44 1865 272861 (self)
> >> 1 South Parks Road, +44 1865 272866 (PA)
> >> Oxford OX1 3TG, UKFax:  +44 1865 272595
> >>
> >
> >
> >
>
> --
> Brian D. Ripley,  [EMAIL PROTECTED]
> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
> University of Oxford, Tel:  +44 1865 272861 (self)
> 1 South Parks Road, +44 1865 272866 (PA)
> Oxford OX1 3TG, UKFax:  +44 1865 272595
>


-- 
Ronggui Huang
Department of Sociology
Fudan University, Shanghai, China

__
R-help@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.