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

Kurt Hornik Kurt.Hornik at wu-wien.ac.at
Wed Apr 5 13:36:41 CEST 2006


>>>>> Prof Brian Ripley writes:

> 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.

> As for

>>> PS, may I suggest to modify page() so that
>>> 'page(getAnywhere("predict.smooth.spline.fit"))' works? DS.

> it is rather tricky.  page() takes a name aka symbol as its argument
> (and is thereby S-compatible), and also works with a bare character
> string (undocumented).  What you have here is a call that does not
> even return a function.  It is more reasonable that
> stats:::predict.smooth.spline.fit should work, and it is also a call.
> I have in the past thought about special-casing that, but it is a
> valid name (you would have to back-quote it, but it does work).  So
> one possible way out would be to use get() on a name and evaluate
> calls, e.g.

> page <- function(x, method = c("dput", "print"), ...)
> {
>      subx <- substitute(x)
>      have_object <- FALSE
>      if(is.call(subx)) {
>          object <- x
>          have_object <- TRUE
>          subx <- deparse(subx)
>      } else {
>          if(is.character(x)) subx <- x
>          else if(is.name(subx)) subx <- deparse(subx)
>          if (!is.character(subx) || length(subx) != 1)
>              stop("'page' requires a name, call or character string")
>          parent <- parent.frame()
>          if(exists(subx, envir = parent, inherits=TRUE)) {
>              object <- get(subx, envir = parent, inherits=TRUE)
>              have_object <- TRUE
>          }
>      }
>      if(have_object) {
>          method <- match.arg(method)
>          file <- tempfile("Rpage.")
>          if(method == "dput")
>              dput(object, file)
>          else {
>              sink(file)
>              print(object)
>              sink()
>          }
>  	file.show(file, title = subx, delete.file = TRUE, ...)
>      } else
>  	stop(gettextf("no object named '%s' to show", subx), domain = NA)
> }

> which also allows 1-element character vectors (and I am not entirely
> sure we want that).

There was a similar issue with prompt() (actually, its default method)
for which I ended up "temporarily" providing the following (argh):

        else {
            name <- substitute(object)
            if (is.name(name)) 
                as.character(name)
            else if (is.call(name) && (as.character(name[[1]]) %in% 
                c("::", ":::", "getAnywhere"))) {
                name <- as.character(name)
                name[length(name)]
            }
            else stop("cannot determine a usable name")
        }

Best
-k



More information about the R-devel mailing list