Skip to content

deparse(substitute(x)) and S3 methods

2 messages · Gavin Simpson

#
Dear List,

I have the following function:

coca <- function(x, ...)
  {
    if(is.null(class(x))) class(x) <- data.class(x)
    UseMethod("coca", x)
  }

and a default method

coca.default <- function(x, y, method = c("predictive", "symmetric"),
                         reg.method = c("simpls", "eigen"), weights =
NULL,
                         n.axes = NULL, symmetric = FALSE, ...)
  {
    ##some checking code here removed###
    nam.dat <- list(namY = deparse(substitute(y)),
                    namX = deparse(substitute(x)))
    method <- match.arg(method)
    if(method == "predictive")
      {
        reg.method <- match.arg(reg.method)
        retval <- switch(reg.method,
                         simpls = predcoca.simpls(y, x, R0 = weights,
                           n.axes = n.axes, nam.dat),
                         eigen = predcoca.eigen(y, x, R0 = weights,
                           n.axes = n.axes, nam.dat))
      } else {
        retval <- symcoca(y, x, n.axes = n.axes, R0 = weights,
                          symmetric = symmetric, nam.dat)
      }
    return(retval)
  }

My problem is with :
    nam.dat <- list(namY = deparse(substitute(y)),
                    namX = deparse(substitute(x)))

deparse(subsitute(x)) and deparse(subsitute(y)) return a textual
representation of x and y.

x and y are both data.frames. I assume this is because they are being
evaluated in coca() and passed on as something different to
coca.default.

I also have coca.formula() so I want to do method dispatch on whether x
is a formula or not, but I want to retain the ability to grab the names
of x and y as specified in the original call.

Can anyone suggest a way round this problem?

Thanks in advance,

Gavin
#
On Wed, 2005-08-03 at 16:28 +0100, Gavin Simpson wrote:
Thanks to Tony Plate (and his simple example - message to self: "follow
the posting guide's advice next time!!!"), I realised that deparse
(substitute( )) was taking place *after* I had modified x and y in
situations where some columns contained all zero values. Moving the
offending lines above the modifying if() statements solves the problem.

So - not a problem with method dispatch at all, except perhaps within my
brain just now...

All the best,

Gav