The R Project SVN R

Rev

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

Rev Author Line No. Line
52846 jmc 1
options(error = recover)
52960 jmc 2
## simple call, only field names
3
fg <- setRefClass("foo", c("bar", "flag"))
4
f1<- new("foo")
5
f1$bar
6
f1 <- fg$new(flag = "testing")
7
f1$bar <- 1
8
stopifnot(identical(f1$bar, 1))
9
fg$methods(showAll = function() c(bar, flag))
10
stopifnot(all.equal(f1$showAll(), c(1, "testing")))
11
 
52903 jmc 12
fg <- setRefClass("foo", list(bar = "numeric", flag = "character"),
52960 jmc 13
            methods = list(
52816 jmc 14
            addToBar = function(incr) {
52960 jmc 15
                b = bar + incr
16
                bar <<- b
52816 jmc 17
                b
18
            }
19
            ))
20
ff = new("foo", bar = 1.5)
21
stopifnot(identical(ff$bar, 1.5))
22
ff$bar <- pi
23
stopifnot(identical(ff$bar, pi))
52903 jmc 24
## test against generator
52816 jmc 25
 
52903 jmc 26
f2 <- fg$new(bar = pi)
27
## identical does not return TRUE if *contents* of env are identical
28
stopifnot(identical(ff$bar, f2$bar), identical(ff$flag, f2$flag))
29
 
52960 jmc 30
f2$flag <- "standard flag"
31
stopifnot(identical(f2$flag, "standard flag"))
52903 jmc 32
 
52960 jmc 33
## fg$lock("flag")
34
## tryCatch(f2$flag <- "other", error = function(e)e)
52816 jmc 35
 
52960 jmc 36
 
37
## add some accessor methods
38
fg$accessors("bar")
39
 
52816 jmc 40
ff$setBar(1:3)
52960 jmc 41
stopifnot(identical(ff$getBar(), 1:3))
52816 jmc 42
 
43
ff$getBar()
44
stopifnot(all.equal(ff$addToBar(1), 2:4))
45
 
52960 jmc 46
 
52903 jmc 47
## Add a method
48
fg$methods(barTimes = function(x) {
49
    "This method multiples field bar by argument x
50
and this string is self-documentation"
51
    setBar(getBar() * x)})
52
 
53
ffbar <- ff$getBar()
54
ff$barTimes(10)
55
stopifnot(all.equal(ffbar * 10, ff$getBar()))
56
ff$barTimes(.1)
57
 
52846 jmc 58
## inheritance.  redefines flag so should fail:
52903 jmc 59
stopifnot(is(tryCatch(setRefClass("foo2", list(b2 = "numeric", flag = "complex"),
52816 jmc 60
            contains = "foo",
52903 jmc 61
            refMethods = list(addBoth = function(incr) {
52816 jmc 62
                addToBar(incr) #uses inherited class method
63
                setB2(getB2() + incr)
52903 jmc 64
                })),
65
          error = function(e)e), "error"))
52846 jmc 66
## but with flag as a subclass of "character", should work
52871 jmc 67
setClass("ratedChar", contains = "character", representation(score = "numeric"))
52903 jmc 68
foo2 <- setRefClass("foo2", list(b2 = "numeric", flag = "ratedChar"),
52846 jmc 69
            contains = "foo",
52960 jmc 70
            methods = list(addBoth = function(incr) {
52846 jmc 71
                addToBar(incr) #uses inherited class method
52960 jmc 72
                b2<<- b2 + incr
52816 jmc 73
                }))
52960 jmc 74
## now lock the flag field; should still allow one write
75
foo2$lock("flag")
52903 jmc 76
f2 <- foo2$new(bar = -3, flag = as("ANY", "ratedChar"), b2 = ff$bar)
52960 jmc 77
## but not a second one
78
stopifnot(is(tryCatch(f2$flag <- "Try again",
79
         error = function(e)e), "error"))
80
f22 <- foo2$new(bar = f2$bar)
81
## same story if assignment follows the initialization
82
f22$flag <- f2$flag
83
stopifnot(is(tryCatch(f22$flag <- "Try again",
84
         error = function(e)e), "error"))
85
## Exporting superclass object
52903 jmc 86
f22 <- fg$new(bar = f2$bar, flag = f2$flag)
87
f2e <- f2$export("foo")
88
stopifnot(identical(f2e$bar, f22$bar), identical(f2e$flag, f22$flag),
89
          identical(class(f2e), class(f22)))
52871 jmc 90
stopifnot(identical(f2$flag,  as("ANY", "ratedChar")), identical(f2$bar, -3),
52816 jmc 91
          all.equal(f2$b2, 2:4+0))
92
f2$addBoth(-1)
93
stopifnot(all.equal(f2$bar, -4), all.equal(f2$b2, 1:3+0))
94
 
52960 jmc 95
## test callSuper()
96
setRefClass("foo3", fields = list(flag2 = "ratedChar"),
97
            contains = "foo2",
98
            methods = list(addBoth = function(incr) {
52846 jmc 99
                callSuper(incr)
52960 jmc 100
                flag2 <<- as(paste(flag, paste(incr, collapse = ", "), sep = "; "), "ratedChar")
52816 jmc 101
                incr
102
            }))
103
 
52960 jmc 104
f2 <- foo2$new(bar = -3, flag = as("ANY", "ratedChar"), b2 =  1:3)
52846 jmc 105
f3 <- new("foo3")
106
f3$import(f2)
107
stopifnot(all.equal(f3$b2, f2$b2), all.equal(f3$bar, f2$bar), all.equal(f3$flag, f2$flag))
52871 jmc 108
f3$addBoth(1)
52960 jmc 109
stopifnot(all.equal(f3$bar, -2), all.equal(f3$b2, 2:4+0),
110
          all.equal(f3$flag2, as("ANY; 1", "ratedChar")))
52871 jmc 111
 
52960 jmc 112
## but the import should have used up the one write for $flag
113
stopifnot(is(tryCatch(f3$flag <- "Try again",
114
         error = function(e)e), "error"))
115
 
116
## a class with an initialize method, and an extra slot
117
setOldClass(c("simple.list", "list"))
118
fg4 <- setRefClass("foo4",
119
            contains = "foo2",
120
            methods = list(
121
              initialize = function(...) {
122
                  .self <- initFields(...)
123
                  .self@made = R.version
124
                  .self
125
              }),
126
            representation = list(made = "simple.list")
127
            )
128
 
129
f4 <- new("foo4", flag = "another test", bar = 1:3)
130
stopifnot(identical(f4@made, R.version))