The R Project SVN R

Rev

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

Rev Author Line No. Line
8504 maechler 1
 
22734 jmc 2
R : Copyright 2003, The R Development Core Team
25033 maechler 3
Version 1.8.0 Under development (unstable) (2003-07-02)
8504 maechler 4
 
5
R is free software and comes with ABSOLUTELY NO WARRANTY.
6
You are welcome to redistribute it under certain conditions.
13855 pd 7
Type `license()' or `licence()' for distribution details.
8504 maechler 8
 
9
R is a collaborative project with many contributors.
13855 pd 10
Type `contributors()' for more information.
8504 maechler 11
 
13855 pd 12
Type `demo()' for some demos, `help()' for on-line help, or
13
`help.start()' for a HTML browser interface to help.
14
Type `q()' to quit R.
8504 maechler 15
 
16
> #### Testing  UseMethod() and even more NextMethod()
17
> ####
18
> 
19
> ###-- Group methods
20
> 
21
> ### Arithmetic "Ops" :
22
> ">.bar" <- function(...) print(">.bar")
23
> ">.foo" <- function(...) print(">.foo")
24
> Ops.foo <- function(...) {
25
+     print("Ops.foo")
26
+     NextMethod()
27
+ }
28
> Ops.bar <- function(...)
29
+     print("Ops.bar")
30
> 
31
> x <- 2:4 ; class(x) <- c("foo", "bar")
32
> y <- 4:2 ; class(y) <- c("bar", "foo")
33
> 
34
> ## The next 4 give a warning each about incompatible methods:
35
> x > y
36
[1] FALSE FALSE  TRUE
37
Warning message: 
38
Incompatible methods (">.foo", ">.bar") for ">" 
39
> y < x # should be the same (warning msg not, however)
40
[1] FALSE FALSE  TRUE
41
Warning message: 
42
Incompatible methods ("Ops.bar", "Ops.foo") for "<" 
43
> x == y
44
[1] FALSE  TRUE FALSE
45
Warning message: 
46
Incompatible methods ("Ops.foo", "Ops.bar") for "==" 
47
> x <= y
48
[1]  TRUE  TRUE FALSE
49
Warning message: 
50
Incompatible methods ("Ops.foo", "Ops.bar") for "<=" 
51
> 
52
> x > 3 ##[1] ">.foo"
53
[1] ">.foo"
54
> 
55
> rm(list=">.foo")
56
> x > 3 #-> "Ops.foo" and ">.bar"
57
[1] "Ops.foo"
58
[1] ">.bar"
59
> 
60
> 
61
> 
62
> ### ------------  was ./mode-methods.R till R ver. 1.0.x ----------------
63
> 
64
> ###-- Using Method Dispatch on "mode" etc :
22734 jmc 65
> ## Tests S3 dispatch with the class attr forced to be data.class
66
> ## Not very relevant when S4 methods are around, but kept for historical interest
8504 maechler 67
> abc <- function(x, ...) {
24536 ripley 68
+     cat("abc: Before dispatching; x has class `", class(x), "':", sep="")
69
+     str(x)
24517 ripley 70
+     UseMethod("abc", x) ## UseMethod("abc") (as in S) fails
8504 maechler 71
+ }
72
> 
73
> abc.default <- function(x, ...) sys.call()
74
> 
75
> "abc.(" <- function(x)
76
+     cat("'(' method of abc:", deparse(sys.call(sys.parent())),"\n")
77
> abc.expression <- function(x)
78
+     cat("'expression' method of abc:", deparse(sys.call(sys.parent())),"\n")
79
> 
80
> abc(1)
24536 ripley 81
abc: Before dispatching; x has class `numeric': num 1
8504 maechler 82
abc.default(1)
83
> e0 <- expression((x))
84
> e1 <- expression(sin(x))
85
> abc(e0)
24536 ripley 86
abc: Before dispatching; x has class `expression':  expression((x))
8504 maechler 87
'expression' method of abc: abc.expression(e0) 
88
> abc(e1)
24536 ripley 89
abc: Before dispatching; x has class `expression':  expression(sin(x))
8504 maechler 90
'expression' method of abc: abc.expression(e1) 
91
> abc(e0[[1]])
25033 maechler 92
abc: Before dispatching; x has class `(': language, mode "(": (x)
13855 pd 93
'(' method of abc: "abc.("(e0[[1]]) 
8504 maechler 94
> abc(e1[[1]])
24536 ripley 95
abc: Before dispatching; x has class `call': language sin(x)
8504 maechler 96
abc.default(e1[[1]])
97
>