From ripley at stats.ox.ac.uk  Wed Apr  5 14:32:19 2006
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Wed, 5 Apr 2006 13:32:19 +0100 (BST)
Subject: [Rd] predict.smooth.spline.fit and Recall() (Was: Re: Return
 function from function and Recall())
In-Reply-To: <59d7961d0604050458uf6ff050r1d714eae5885bcea@mail.gmail.com>
References: <59d7961d0604050225g4b3235fby3aa13af460fa38f7@mail.gmail.com> 
	<Pine.LNX.4.64.0604051109100.12918@gannet.stats.ox.ac.uk>
	<59d7961d0604050458uf6ff050r1d714eae5885bcea@mail.gmail.com>
Message-ID: <Pine.LNX.4.64.0604051313110.20233@gannet.stats.ox.ac.uk>

On Wed, 5 Apr 2006, Henrik Bengtsson wrote:

> On 4/5/06, Prof Brian Ripley <ripley at stats.ox.ac.uk> wrote:
>> On Wed, 5 Apr 2006, Henrik Bengtsson wrote:
>>
>>> Hi,
>>>
>>> forget about the below details.  It is not related to the fact that
>>> the function is returned from a function.  Sorry about that.  I've
>>> been troubleshooting soo much I've been shoting over the target.  Here
>>> is a much smaller reproducible example:
>>>
>>> x <- 1:10
>>> y <- 1:10 + rnorm(length(x))
>>> sp <- smooth.spline(x=x, y=y)
>>> ypred <- predict(sp$fit, x)
>>> # [1]  2.325181  2.756166  ...
>>> ypred2 <- predict(sp$fit, c(0,x))
>>> # Error in Recall(object, xrange) : couldn't find
>>> # function "predict.smooth.spline.fit"
>>
>> It seems Recall is not searching (via findFun) from the right environment,
>> but at a quick glance it is not obvious to me why.
>> You can replace Recall by predict.smooth.spline.fit for now.
>
> More troubleshooting shows that by dispatching directly on 'sp' and
> not 'sp$fit' works.  The reason that I do not want to do this is
> related to my questions yesterday that I want to keep the memory usage
> down and 'sp' hold quite some extra data even with keep.data=FALSE.

Ahah!  So you are not using this as intended, and I had not spotted this. 
Now I understand: you are calling predict, which finds 
predict.smooth.spline.fit via method registration, and dispatches it in 
the environment used to call predict, .GlobalEnv, where of course it is 
not visible. Then Recall uses that environment, but attempts to call the 
method directly.

If you call this as intended, predict is called from the body of 
predict.smooth.spline where predict.smooth.spline.fit is visible.

I am afraid that you should not be expecting to be able to subvert things 
this way.

[...]

Now, I was a little surprised that Recall() needed to do a lookup, but R's 
context only contains the name of the called function and not (a pointer 
to) the function.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
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, UK                Fax:  +44 1865 272595



[Rd] predict.smooth.spline.fit and Recall() (Was: Re: Return function from function and Recall())
Prof Brian Ripley