outer fails with group generic operations on factors (PR#166)
Peter Dalgaard BSA
p.dalgaard@biostat.ku.dk
14 Apr 1999 10:21:40 +0200
Martin Maechler <maechler@stat.math.ethz.ch> writes:
> 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 !
..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:
> cde<-function(x,...){x<-x*2;UseMethod("print")}
> cde(2)
[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:
> x<-1
> y<-.Alias(x)
> class(x)<-'foo'
> y
[1] 1
attr(,"class")
[1] "foo"
I think this is related to evaluation of promises:
> f<-function(x)substitute(x)
> f(sin(1))
sin(1)
> f<-function(x){x;substitute(x)}
> f(sin(1))
sin(1)
> f<-function(x){class(x)<-'foo';substitute(x)}
> f(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()
--
O__ ---- Peter Dalgaard Blegdamsvej 3
c/ /'_ --- Dept. of Biostatistics 2200 Cph. N
(*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk) FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._