Re: [Rd] and Recall() (Was: Re: Return function from function and Recall())

From: Prof Brian Ripley <>
Date: Wed 05 Apr 2006 - 16:44:46 GMT

On Wed, 5 Apr 2006, Henrik Bengtsson wrote:

> On 4/5/06, Prof Brian Ripley <> wrote:

>> On Wed, 5 Apr 2006, Henrik Bengtsson wrote:
>>> On 4/5/06, Prof Brian Ripley <> 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 ""
>>>> 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 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
>> Ahah! So you are not using this as intended, and I had not spotted this.
> Neither did I ;)

>> Now I understand: you are calling predict, which finds
>> 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 is visible.
>> I am afraid that you should not be expecting to be able to subvert things
>> this way.
> One could argue that it should not be possible to dispatch to a
> non-exported method the way I did.  If UseMethod() would use the same
> search path as the calling expression in the parent frame (and not
> from "within" the environment where the generic function lives),

It *does* do the former: method dispatch is dynamically scoped. NB: 'via method registration'.

> then the predict() would fail right away.  However, I think it is a very 
> risky business to make such modifications to UseMethod(), because there 
> are probably unknown side effects to this.

One could argue that the particular method should not have been registered, so it could only be found from within the namespace.

However, there are several other examples (such as cophenetic.dendrogram) which are intended to work and fall foul of the same trap.

Brian D. Ripley,        
Professor of Applied Statistics,
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________ mailing list
Received on Thu Apr 06 03:10:55 2006

This archive was generated by hypermail 2.1.8 : Wed 05 Apr 2006 - 20:17:28 GMT