Skip to content

outer fails with group generic operations on factors (PR#166)

5 messages · Brian Ripley, Peter Dalgaard, Martin Maechler

#
B <- A <- factor(c("a", "b"))
outer(A, B, "!=")
Warning: "FUN" not meaningful for factors
     [,1] [,2]
[1,]   NA   NA
[2,]   NA   NA

Now, this used to work in 0.63.2, but someone `improved' outer. There
it did an implicit as.numeric. The problem is that get in match.fun does
not understand group generics, and gets

Browse[1]> FUN
.Primitive("!=")

This _should_ launch Ops.factor, but does not.
[1] FALSE FALSE
Warning: "" not meaningful for factors
[1] NA NA



This causes misclass.tree(, detail=F) in package tree to fail.

--please do not edit the information below--

Version:
 platform = sparc-sun-solaris2.6
 arch = sparc
 os = solaris2.6
 system = sparc, solaris2.6
 status = 
 status.rev = 0
 major = 0
 minor = 64.0
 year = 1999
 month = April
 day = 8
 language = R

Search Path:
 .GlobalEnv, Autoloads, package:base
#
ripley@stats.ox.ac.uk writes:
Actually, it *does* launch Ops.factor, but it gets confused by garbage
in .Generic. The bug is this line in DispatchGroup:

    sprintf(generic, "%s", CHAR(PRINTNAME(CAR(call))));

which presumably should rather look at the "op" arg. 

I'll have a closer look.
#
Peter Dalgaard BSA <p.dalgaard@biostat.ku.dk> writes:
It *apparently* works to change the above line to:

 sprintf(generic, "%s", PRIMNAME(op));

"make check" completes (haven't checked the output yet), but can we
*really* be sure that "op" is a .Primitive at that point?

Anyone know better?
#
Which reminds me of another long-standing  
UseMethod / NextMethod / Dispatch problem that I've never traced/solved..

Look at the  $RHOME/tests/mode-methods.R   file !
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Martin Maechler <maechler@stat.math.ethz.ch> writes:
..i.e. this:
abc <- function(x, ...) {
    if (is.null(class(x))) class(x) <- data.class(x)
    UseMethod("abc")
}

will dispatch on the data class in S but not in R.

This puzzles me, because normally you can't mess with the arguments of
a generic function, in *either* language:
[1] 2

So, either it's because class<- is non-duplicating in S, or S is doing
something *really* strange here. 

Hmm, then again: class<- *is* non-duplicating in R:
[1] 1
attr(,"class")
[1] "foo"

I think this is related to evaluation of promises:
sin(1)
sin(1)
[1] 0.841471
attr(,"class")
[1] "foo"

Notice that just forcing evaluation of x does not destroy the link
between x and the argument expression, but changing its class does.
This is probably what shouldn't happen.

You can get the dispatching done by using

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

which (seemingly) works in both languages, at least as long as you
don't have to mess with sys.parent()