[Rd] Alternative to eval(cl, parent.frame()) ?

Bjørn-Helge Mevik b.h.mevik at usit.uio.no
Sat Jul 13 10:56:29 CEST 2013


Dear developeRs,

I maintain a package 'pls', which has a main fit function mvr(), and
functions plsr() and pcr() which are meant to take the same arguments as
mvr() and do exactly the same, but have different default values for the
'method' argument.  The three functions are all exported from the name
space.

In the 'pre namespace' era, I took inspiration from lm() and implemented
it like this:

plsr <- function(..., method = pls.options()$plsralg) {
    cl <- match.call()
    cl$method <- match.arg(method, c("kernelpls", "widekernelpls", "simpls",
                                     "oscorespls", "model.frame"))
    cl[[1]] <- as.name("mvr")
    res <- eval(cl, parent.frame())
    ...


Recently, Prof. Brian Ripley kindly pointed out that this doesn't work
properly when the 'pls' package in not attached:

> data(yarn, package='pls')
> pls::plsr(density ~ NIR, 6, data = yarn, validation = "CV")
<environment: R_GlobalEnv>
Error in eval(expr, envir, enclos) : could not find function "mvr"

I first believed that

    cl[[1]] <- as.name("pls::mvr")

would fix the problem, but that did not help.  I have found that the
following seems to work:

plsr <- function(..., method = pls.options()$plsralg) {
    cl <- match.call()
    cl$method <- match.arg(method, c("kernelpls", "widekernelpls", "simpls",
                                     "oscorespls", "model.frame"))
    arguments <- as.list(cl)[-1]
    res <- do.call(mvr, arguments, envir = parent.frame())
    ...

However, if I understand correctly, this will evaluate the arguments
before handing them over to mvr().  Currently, mvr() doesn't need the
unevaluated arguments, but if it were to, this would be a problem.

Is there an 'R best practice' for achieving what I want (several
versions of the same function, with different default value for an
argument)?

-- 
Regards,
Bjørn-Helge Mevik



More information about the R-devel mailing list