An answer to a query about a mysterious problem.

I think you are having trouble with a subtlety called 'evaluation
frames', which is described on pp 109-110 of Venables and Ripley.  I
tried to recreate the problem with a simple example:


##### an illustration of evaluation frame problems 
> junk
function(word)
{
        word2 <- word
        noword <- "nothing"
        thing <- function(x)
        {
                print(word2)
        }
        thing(noword)
}
> junk("hello")
Error in thing(noword): Object "word2" not found
Dumped
> junk2
function(word)
{
        assign("word2", word, frame = 1)
        noword <- "nothing"
        thing <- function(x)
        {
                print(word2)
        }
        thing(noword)
}
> junk2("hello")
[1] "hello"
[1] "hello"
##################

In both cases I put the definition of the new function 'thing' inside
another function definition.  

First, what happens in 'junk'?  Unfortunately, the definition of thing
creates a new evaluation frame: an environment where names get matched
up with values.  When Splus sees an object name it tries to find its
value in the 'local frame', which in this case refers to everything
defined within the definition of 'thing'.  There is no object called
word2 defined within the 'thing' evaluation frame, so (see the
explanation near the top of page 108 of Venables and Ripley) it looks
for an object defined within frame 1.  Here I get a litle confused,
because I thought frame 1 was the frame established by the 'junk'
definition.  Apparently not.  (I wonder which frame it is?)  If it
doesn't find the object in frame 1 it then looks in frame 0, which is
the evaluation frame created when the Splus session begins.  There is
no word2 defined in frame 0, so then Splus looks through the search()
path.  Nowhere is word2 defined, so Splus complains that the object is
not found.

For the definition of junk2 I explicitly assigned word2 a value within
frame 1, so that Splus could find it.  It did.

As you can see, I am not really completely confident in my understanding.  I
need to do some experiments (with the functions described on pp 97-103
of V&R) to figure out which frame is created by junk.


David Pollard