Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 109 lines (103 sloc) 3.306 kb
c22e857 eliminated simple-tests; activated mode-methods; some additions
maechler authored
1
069e5c0 update reference output
ripley authored
2 R Under development (unstable) (2012-03-05 r58592)
3 Copyright (C) 2012 The R Foundation for Statistical Computing
6c644fe use backticks more consistently when deparsing.
ripley authored
4 ISBN 3-900051-07-0
069e5c0 update reference output
ripley authored
5 Platform: x86_64-unknown-linux-gnu (64-bit)
c22e857 eliminated simple-tests; activated mode-methods; some additions
maechler authored
6
7 R is free software and comes with ABSOLUTELY NO WARRANTY.
8 You are welcome to redistribute it under certain conditions.
6c644fe use backticks more consistently when deparsing.
ripley authored
9 Type 'license()' or 'licence()' for distribution details.
c22e857 eliminated simple-tests; activated mode-methods; some additions
maechler authored
10
11 R is a collaborative project with many contributors.
6c644fe use backticks more consistently when deparsing.
ripley authored
12 Type 'contributors()' for more information and
13 'citation()' on how to cite R or R packages in publications.
c22e857 eliminated simple-tests; activated mode-methods; some additions
maechler authored
14
6c644fe use backticks more consistently when deparsing.
ripley authored
15 Type 'demo()' for some demos, 'help()' for on-line help, or
16 'help.start()' for an HTML browser interface to help.
17 Type 'q()' to quit R.
c22e857 eliminated simple-tests; activated mode-methods; some additions
maechler authored
18
d442f16 sent previous version by mistake
jmc authored
19 > #### Testing UseMethod() and even more NextMethod()
20 > #### --------------------
21 > #### i.e., S3 methods *only*. For S4, see reg-S4.R
22 > ## ~~~~~~~~
18e2e98 revisions and addition of referenceMethods() generic
jmc authored
23 >
d442f16 sent previous version by mistake
jmc authored
24 > ###-- Group methods
18e2e98 revisions and addition of referenceMethods() generic
jmc authored
25 >
d442f16 sent previous version by mistake
jmc authored
26 > ## previous versions used print() and hit an auto-printing bug.
18e2e98 revisions and addition of referenceMethods() generic
jmc authored
27 >
d442f16 sent previous version by mistake
jmc authored
28 > ### Arithmetic "Ops" :
29 > ">.bar" <- function(...) {cat("using >.bar\n"); FALSE}
30 > ">.foo" <- function(...) {cat("using >.foo\n"); TRUE}
31 > Ops.foo <- function(...) {
32 + cat("using Ops.foo\n")
33 + NextMethod()
fb8f328 example demonstrated an auto-printing bug
ripley authored
34 + }
d442f16 sent previous version by mistake
jmc authored
35 > Ops.bar <- function(...) {
36 + cat("using Ops.bar\n")
37 + TRUE
c22e857 eliminated simple-tests; activated mode-methods; some additions
maechler authored
38 + }
18e2e98 revisions and addition of referenceMethods() generic
jmc authored
39 >
d442f16 sent previous version by mistake
jmc authored
40 > x <- 2:4 ; class(x) <- c("foo", "bar")
41 > y <- 4:2 ; class(y) <- c("bar", "foo")
42 >
43 > ## The next 4 give a warning each about incompatible methods:
44 > x > y
45 [1] FALSE FALSE TRUE
46 Warning message:
47 Incompatible methods (">.foo", ">.bar") for ">"
48 > y < x # should be the same (warning msg not, however)
49 [1] FALSE FALSE TRUE
50 Warning message:
51 Incompatible methods ("Ops.bar", "Ops.foo") for "<"
52 > x == y
53 [1] FALSE TRUE FALSE
54 Warning message:
55 Incompatible methods ("Ops.foo", "Ops.bar") for "=="
56 > x <= y
57 [1] TRUE TRUE FALSE
58 Warning message:
59 Incompatible methods ("Ops.foo", "Ops.bar") for "<="
60 >
61 > x > 3 ##[1] ">.foo"
62 using >.foo
63 [1] TRUE
64 >
65 > rm(list=">.foo")
66 > x > 3 #-> "Ops.foo" and ">.bar"
67 using Ops.foo
68 using >.bar
18e2e98 revisions and addition of referenceMethods() generic
jmc authored
69 [1] FALSE
70 >
71 >
72 >
d442f16 sent previous version by mistake
jmc authored
73 > ### ------------ was ./mode-methods.R till R ver. 1.0.x ----------------
18e2e98 revisions and addition of referenceMethods() generic
jmc authored
74 >
d442f16 sent previous version by mistake
jmc authored
75 > ###-- Using Method Dispatch on "mode" etc :
76 > ## Tests S3 dispatch with the class attr forced to be data.class
77 > ## Not very relevant when S4 methods are around, but kept for historical interest
78 > abc <- function(x, ...) {
79 + cat("abc: Before dispatching; x has class `", class(x), "':", sep="")
80 + str(x)
81 + UseMethod("abc", x) ## UseMethod("abc") (as in S) fails
18e2e98 revisions and addition of referenceMethods() generic
jmc authored
82 + }
c22e857 eliminated simple-tests; activated mode-methods; some additions
maechler authored
83 >
d442f16 sent previous version by mistake
jmc authored
84 > abc.default <- function(x, ...) sys.call()
85 >
86 > "abc.(" <- function(x)
87 + cat("'(' method of abc:", deparse(sys.call(sys.parent())),"\n")
88 > abc.expression <- function(x)
89 + cat("'expression' method of abc:", deparse(sys.call(sys.parent())),"\n")
90 >
91 > abc(1)
92 abc: Before dispatching; x has class `numeric': num 1
93 abc.default(1)
94 > e0 <- expression((x))
95 > e1 <- expression(sin(x))
96 > abc(e0)
97 abc: Before dispatching; x has class `expression': expression((x))
98 'expression' method of abc: abc.expression(e0)
99 > abc(e1)
100 abc: Before dispatching; x has class `expression': expression(sin(x))
101 'expression' method of abc: abc.expression(e1)
102 > abc(e0[[1]])
103 abc: Before dispatching; x has class `(': language, mode "(": (x)
104 '(' method of abc: `abc.(`(e0[[1]])
105 > abc(e1[[1]])
106 abc: Before dispatching; x has class `call': language sin(x)
107 abc.default(e1[[1]])
c22e857 eliminated simple-tests; activated mode-methods; some additions
maechler authored
108 >
Something went wrong with that request. Please try again.