The R Project SVN R

Rev

Rev 50986 | Rev 84105 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 50986 Rev 83997
Line 1... Line 1...
1
#### Testing  UseMethod() and even more NextMethod()
1
#### Testing  UseMethod() and even more NextMethod()
2
#### -------------------- 
2
#### --------------------
3
#### i.e.,  S3 methods *only*. For S4, see  reg-S4.R
3
#### i.e.,  S3 methods *only*. For S4, see  reg-S4.R
4
##                                          ~~~~~~~~
4
##                                          ~~~~~~~~
5
 
5
 
6
###-- Group methods
6
###-- Group methods
7
 
7
 
Line 58... Line 58...
58
e1 <- expression(sin(x))
58
e1 <- expression(sin(x))
59
abc(e0)
59
abc(e0)
60
abc(e1)
60
abc(e1)
61
abc(e0[[1]])
61
abc(e0[[1]])
62
abc(e1[[1]])
62
abc(e1[[1]])
-
 
63
 
-
 
64
 
-
 
65
## Some tests for `@` dispatching
-
 
66
## make sure that
-
 
67
## - `@` evals the first args only once,
-
 
68
## -  doesn't dispatch for S4
-
 
69
## -  works on `.Data` even for nonS4 objects
-
 
70
 
-
 
71
x <- structure(list(), class = "foo", prop1 = 'prop1val')
-
 
72
registerS3method("@", "foo",
-
 
73
    function(x, name) {
-
 
74
        stopifnot(typeof(name) == "character", length(name) == 1L)
-
 
75
        cat(sprintf("called `@.foo`(x = %s, name = '%s')\n",
-
 
76
                     deparse1(substitute(x), "\n"), name))
-
 
77
        attr(x, name, TRUE)
-
 
78
    }
-
 
79
)
-
 
80
x@prop1
-
 
81
 
-
 
82
abc <- x
-
 
83
abc@prop1
-
 
84
 
-
 
85
{
-
 
86
    cat("new x\n")
-
 
87
    structure(list(), class = "foo", prop1 = 'prop1val')
-
 
88
}@prop1
-
 
89
 
-
 
90
makeActiveBinding("ax", function(x) {
-
 
91
    cat("evaluating ax\n")
-
 
92
    get("x", envir = parent.frame())
-
 
93
}, environment())
-
 
94
 
-
 
95
ax@prop1
-
 
96
 
-
 
97
stopifnot(exprs = {
-
 
98
    identical( x@prop1, "prop1val")
-
 
99
    identical(ax@prop1, "prop1val")
-
 
100
 
-
 
101
    identical(letters@.Data, letters)
-
 
102
})
-
 
103
 
-
 
104
try(letters@foo) # error
-
 
105
 
-
 
106
# doesn't dispatch for S4
-
 
107
setClass("Person",
-
 
108
  slots = c(
-
 
109
    name = "character",
-
 
110
    age = "numeric"
-
 
111
  )
-
 
112
)
-
 
113
 
-
 
114
`@.Person` <- function(x, name) {
-
 
115
  stop("called @.Person()\n")
-
 
116
}
-
 
117
 
-
 
118
p <- new("Person", name = "Who", age = -1)
-
 
119
stopifnot(p@name == "Who")