[R] get: problem with environments

Thomas Lumley tlumley at uw.edu
Thu Oct 11 00:52:14 CEST 2012

On Thu, Oct 11, 2012 at 11:18 AM, R. Michael Weylandt
<michael.weylandt at gmail.com> wrote:

> Thanks Prof Lumley,
> I'm still not sure how this gets to the call stack of 5 or 6 the OP
> reported or the difference between GUI & Terminal. Any thoughts there?

I don't see how the terminal version is getting 5 and 6 rather than 1
and 2, but my testfun2() lets the OP do what he was originally trying
to do.

The difference between testfun() and print(testfun()) (ie, 1 vs 2, or
5 vs 6) is because of lazy evaluation: in print(testfun()), testfun()
is called from inside print() when it needs the value to do method

I can't reproduce the value of 5, so I can't help much. For anyone
wanting to experiment further, it would probably be simpler to use

testfun<-function() sys.nframe()

rather than looking at whether a variable is found or not.  I can't
see how testfun() typed at the global command prompt can return
anything other than 1, but maybe something is getting in between the
console and the evaluator.  For example:
> testfun()
[1] 1
> print(testfun())
[1] 2
> capture.output(testfun())
[1] "[1] 6"

I don't see why a pure console program should do this on Linux, though.


Thomas Lumley
Professor of Biostatistics
University of Auckland

More information about the R-help mailing list