Skip to content
Prev 9170 / 63421 Next

setGeneric

I think this is a consequence of the extra context added to make methods
work right with R lexical scoping, namespaces, etc.  Or a subtlety in
R's definition of missing()?

The problem is that somehow the default expression for argument `ncol'
makes that argument appear NOT to be missing.  But an attempt to
evaluate it fails because the local variable `n' hasn't been defined
yet.

The following debugging snippets, using trace on the method for
"numeric", show what's happening.  But as to why, we need some expert
help! (Luke?)

R> trace("diag", sig="numeric", browser)
[1] "diag"
R> diag(2)
Tracing diag(2) on entry 
Called from: diag(2)
Browse[1]> missing(x)
[1] FALSE
Browse[1]> missing(nrow)
[1] TRUE
Browse[1]> missing(ncol)
[1] FALSE
Browse[1]> ncol
Error during wrapup: Object "n" not found

R> selectMethod("diag", "numeric")
Method Definition (Class "MethodDefinitionWithTrace"):

function (x = 1, nrow, ncol = n) 
{
......

By the way, while this example seems to be some obscure glitch, there is
a real potential for code that relies heavily on lazy evaluation to
break when functions are made generic, just for the simple reason that
arguments involved in method signatures (ANY defined method signature)
may need to be evaluated to dispatch the method, before they would
normally be needed in the body of the original non-generic function.

John
Roger Koenker wrote: