The R Project SVN R

Rev

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

Rev 52784 Rev 52823
Line 1... Line 1...
1
 
1
 
2
R version 2.12.0 Under development (unstable) (2010-08-11 r52699)
2
R version 2.12.0 Under development (unstable) (2010-08-28 r52822)
3
Copyright (C) 2010 The R Foundation for Statistical Computing
3
Copyright (C) 2010 The R Foundation for Statistical Computing
4
ISBN 3-900051-07-0
4
ISBN 3-900051-07-0
5
Platform: x86_64-unknown-linux-gnu (64-bit)
5
Platform: x86_64-unknown-linux-gnu (64-bit)
6
 
6
 
7
R is free software and comes with ABSOLUTELY NO WARRANTY.
7
R is free software and comes with ABSOLUTELY NO WARRANTY.
Line 176... Line 176...
176
Object of class "myFrame"
176
Object of class "myFrame"
177
  x          y z
177
  x          y z
178
1 1 -0.6264538 y
178
1 1 -0.6264538 y
179
2 2  0.1836433 f
179
2 2  0.1836433 f
180
Slot "timestamps":
180
Slot "timestamps":
181
[1] "2010-08-11 09:41:05 BST" "2010-08-11 09:41:05 BST"
181
[1] "2010-08-29 08:25:45 BST" "2010-08-29 08:25:45 BST"
182
 
182
 
183
> 
183
> 
184
> 
184
> 
185
> setClass("myDateTime", contains = "POSIXt")
185
> setClass("myDateTime", contains = "POSIXt")
186
[1] "myDateTime"
186
[1] "myDateTime"
Line 1810... Line 1810...
1810
[1] TRUE
1810
[1] TRUE
1811
> ## End Don't show
1811
> ## End Don't show
1812
> 
1812
> 
1813
> 
1813
> 
1814
> cleanEx()
1814
> cleanEx()
-
 
1815
> nameEx("refClass")
-
 
1816
> ### * refClass
-
 
1817
> 
-
 
1818
> flush(stderr()); flush(stdout())
-
 
1819
> 
-
 
1820
> ### Name: ReferenceClasses
-
 
1821
> ### Title: Creating Classes With Fields Treated by Reference
-
 
1822
> ### Aliases: ReferenceClasses setRefClass refClassFields refClassMethods
-
 
1823
> ###   refClassRepresentation-class refClass-class refObject-class
-
 
1824
> ###   classMethodDef-class show,classMethodDef-method
-
 
1825
> ###   show,refClassRepresentation-method
-
 
1826
> ### Keywords: programming classes
-
 
1827
> 
-
 
1828
> ### ** Examples
-
 
1829
> 
-
 
1830
> ## a simple editor for matrix objects.  Method  edit() changes some
-
 
1831
> ## range of values; method undo() undoes the last edit.
-
 
1832
> setRefClass("matrixEditor",
-
 
1833
+       fieldClasses = list( data = "matrix",
-
 
1834
+         edits = "list"),
-
 
1835
+       classMethods = list(
-
 
1836
+         edit = function(i, j, value) {
-
 
1837
+           x <- getData()
-
 
1838
+           backup <-
-
 
1839
+             list(i, j, x[i,j])
-
 
1840
+           setEdits(c(list(backup),
-
 
1841
+             getEdits()))
-
 
1842
+           x[i,j] <- value
-
 
1843
+           setData(x)
-
 
1844
+           invisible(value)
-
 
1845
+         },
-
 
1846
+         undo = function() {
-
 
1847
+           prev <- getEdits()
-
 
1848
+           if(length(prev)) prev <- prev[[1]]
-
 
1849
+           else stop("No more edits to undo")
-
 
1850
+           edit(prev[[1]], prev[[2]], prev[[3]])
-
 
1851
+           ## trim the edits list
-
 
1852
+           setEdits(
-
 
1853
+             getEdits()[-(1:2)])
-
 
1854
+           invisible(prev)
-
 
1855
+         },
-
 
1856
+         save = function(file) {
-
 
1857
+           base::save(.self, file = file)
-
 
1858
+         }
-
 
1859
+       )
-
 
1860
+     )
-
 
1861
> xMat <- matrix(1:12,4,3)
-
 
1862
> xx <- new("matrixEditor", data = xMat)
-
 
1863
> xx$edit(2, 2, 0)
-
 
1864
> xx$data
-
 
1865
     [,1] [,2] [,3]
-
 
1866
[1,]    1    5    9
-
 
1867
[2,]    2    0   10
-
 
1868
[3,]    3    7   11
-
 
1869
[4,]    4    8   12
-
 
1870
> xx$undo()
-
 
1871
> stopifnot(all.equal(xx$data, xMat))
-
 
1872
> ## Don't show: 
-
 
1873
> tf <- tempfile()
-
 
1874
> xx$save(tf) #$
-
 
1875
> load(tf)
-
 
1876
> unlink(tf)
-
 
1877
> stopifnot(identical(xx$data, .self$data))
-
 
1878
> ## End Don't show
-
 
1879
> ## A version of the classMethods= argument above that
-
 
1880
> ## is a little simpler and faster but only works with
-
 
1881
> ## the implementation in R, not with interfaces:
-
 
1882
> localMethods <- list(
-
 
1883
+      edit = function(i, j, value) {
-
 
1884
+          backup <-
-
 
1885
+              list(i, j, data[i,j])
-
 
1886
+          edits <<- c(list(backup),
-
 
1887
+                      edits)
-
 
1888
+          data[i,j] <<- value
-
 
1889
+          invisible(value)
-
 
1890
+      },
-
 
1891
+      undo = function() {
-
 
1892
+          prev <- getEdits()
-
 
1893
+          if(length(prev)) prev <- prev[[1]]
-
 
1894
+          else stop("No more edits to undo")
-
 
1895
+          edit(prev[[1]], prev[[2]], prev[[3]])
-
 
1896
+          ## trim the edits list
-
 
1897
+          length(edits) <<- length(edits) - 2
-
 
1898
+          invisible(prev)
-
 
1899
+      },
-
 
1900
+      save = function(file) {
-
 
1901
+          base::save(.self, file = file)
-
 
1902
+      }
-
 
1903
+ )
-
 
1904
> setRefClass("matrixEditor2",
-
 
1905
+       fieldClasses = list( data = "matrix",
-
 
1906
+         edits = "list"),
-
 
1907
+       classMethods = localMethods)
-
 
1908
> 
-
 
1909
> ## Inheriting a reference class:  a matrix viewer
-
 
1910
> setRefClass("matrixViewer", contains = "matrixEditor",
-
 
1911
+     classMethods = list( view = function() matplot(data),
-
 
1912
+         edit = # invoke previous method, then replot
-
 
1913
+           function(i, j, value) {
-
 
1914
+             edit.prev(i, j, value)
-
 
1915
+             view()
-
 
1916
+           }))
-
 
1917
> 
-
 
1918
> ## Don't show: 
-
 
1919
> ff = new("matrixEditor2", data = xMat)
-
 
1920
> ff$edit(2,2,0)
-
 
1921
> ff$data
-
 
1922
     [,1] [,2] [,3]
-
 
1923
[1,]    1    5    9
-
 
1924
[2,]    2    0   10
-
 
1925
[3,]    3    7   11
-
 
1926
[4,]    4    8   12
-
 
1927
> ff$undo()
-
 
1928
> stopifnot(all.equal(ff$data, xMat))
-
 
1929
> removeClass("matrixEditor")
-
 
1930
[1] TRUE
-
 
1931
> removeClass("matrixEditor2")
-
 
1932
[1] TRUE
-
 
1933
> removeClass("matrixViewer")
-
 
1934
[1] TRUE
-
 
1935
> ## End Don't show
-
 
1936
> 
-
 
1937
> 
-
 
1938
> 
-
 
1939
> cleanEx()
1815
> nameEx("representation")
1940
> nameEx("representation")
1816
> ### * representation
1941
> ### * representation
1817
> 
1942
> 
1818
> flush(stderr()); flush(stdout())
1943
> flush(stderr()); flush(stdout())
1819
> 
1944
>