The R Project SVN R

Rev

Rev 69213 | Rev 88267 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
54325 jmc 1
## reset inherited methods of group members
2
## (contributed by Martin Morgan, 2011-2-9)
3
setClass("A", representation("numeric"))
4
a <- new("A")
5
 
6
setMethod("Logic", c("A", "A"), function(e1, e2) FALSE)
7
res0 <- a & a                           # inherit &,A,A-method
8
setMethod("Logic", c("A", "A"), function(e1, e2) TRUE)
9
stopifnot(a & a)
10
 
11
removeMethod("Logic", c("A", "A"))
12
stopifnot(logical() == a & a)
13
 
14
removeClass("A")
58493 maechler 15
 
16
### Find inherited group methods:
70274 maechler 17
if(require(Matrix)) { ## , lib.loc = .Library
69213 ripley 18
    sm <- selectMethod("-", c("dgCMatrix", "numeric"))# direct match with "Arith"
19
    s2 <- selectMethod("-", c("dtCMatrix", "numeric"))# ambiguity match with "Arith"
20
    stopifnot(sm@generic == "Arith", s2@generic == "Arith")
21
}
58511 maechler 22
## was not ok in R 2.14.x
58493 maechler 23
 
66423 jmc 24
## some tests of callGeneric().  It's reccommended for use with group generics
25
setGeneric("f1", signature=c("a"),
26
           function(..., a) standardGeneric("f1"))
27
setMethod("f1", c(a="ANY"), function(..., a) list(a=a, ...))
28
setMethod("f1", c(a="missing"), function(..., a) callGeneric(a=1, ...))
29
f2 <- function(b,c,d, a) {
30
    if (missing(a))
31
        f1(b=b, c=c, d=d)
32
    else
33
        f1(a=a, b=b, c=c, d=d)
34
}
35
 
36
## use callGeneric both directly (f1) and indirectly (f2)
37
## Latter failed pre rev. 66408; Bug ID 15937
66586 maechler 38
stopifnot(identical(c(1,2,3,4), as.vector(unlist(f1(2,3,4)))))
39
stopifnot(identical(c(1,2,3,4), as.vector(unlist(f2(2,3,4)))))
66423 jmc 40
 
41
## test callGeneric() with no arguments.  This is rarely used
42
## because nearly all applications use the groups Ops, etc.
43
## whose members are primitives => must supply args to callGeneric
44
 
45
Hide <- setClass("Hide", slots = c(data = "vector"), contains = "vector")
46
 
47
unhide <- function(obj)
48
    obj@data
49
 
50
setGeneric("%p%", function(e1, e2) e1 + e2, group = "Ops2")
51
setGeneric("%gt%", function(e1, e2) e1 > e2, group = "Ops2")
52
 
53
setGroupGeneric("Ops2", function(e1,e2)NULL, knownMembers = c("%p%","%gt%"))
54
 
55
setMethod("Ops2", c("Hide", "Hide"),
56
          function(e1, e2) {
57
              e1 <- unhide(e1)
58
              e2 <- unhide(e2)
59
              callGeneric()
60
          })
61
 
62
setMethod("Ops2", c("Hide", "vector"),
63
          function(e1, e2) {
64
              e1 <- unhide(e1)
65
              callGeneric()
66
          })
67
setMethod("Ops2", c("vector", "Hide"),
68
          function(e1, e2) {
69
              e2 <- unhide(e2)
70
              callGeneric()
71
          })
72
 
73
h1 <- Hide(data = 1:10)
74
h2 <- Hide(data = (1:10)*.5+ 0.5)
75
 
76
stopifnot(all.equal(h1%p%h2, h1@data + h2@data))
77
stopifnot(all.equal(h1 %gt% h2, h1@data > h2@data))
78
 
79
removeClass("Hide")
80
for(g in c("f1", "%p%", "%gt%", "Ops2"))
81
    removeGeneric(g)
82
 
83