[Rd] Reference classes: accessor functions via 'getRefClass(...)$accessors(...)'
Hi there, I'd like to choose between an "static" and "dynamic" access of a reference class field, say 'a'. myObj <- getRefClass("Blabla")$new() Static: myObj$a Dynamic: myObj$a.get() where the function retrieves the data from a database (or some other location), stores it to a buffer and optionally updates the static field value 'a'. I've set up such a method 'a.get()' where I can actually decide between static and dynamic runmode (so the method also wraps the call 'myObj$a' for the static runmode). Now I saw that setting up such accessor methods (get/set) is already done for me if I use 'getRefClass("Blabla")$accessors(.)'. I just don't understand what exactly I have to do there, because this results in an error: 'getRefClass("Blabla")$accessors("a")' Can anyone point me to the correct use of 'getRefClass(.)$accessors(.)'? Thanks a lot, Janko [[alternative HTML version deleted]] __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Reference classes: accessor functions via 'getRefClass(...)$accessors(...)'
Le 17/11/10 13:07, Janko Thyson a écrit : Hi there, I'd like to choose between an "static" and "dynamic" access of a reference class field, say 'a'. myObj<- getRefClass("Blabla")$new() Static: myObj$a Dynamic: myObj$a.get() where the function retrieves the data from a database (or some other location), stores it to a buffer and optionally updates the static field value 'a'. I've set up such a method 'a.get()' where I can actually decide between static and dynamic runmode (so the method also wraps the call 'myObj$a' for the static runmode). Now I saw that setting up such accessor methods (get/set) is already done for me if I use 'getRefClass("Blabla")$accessors(.)'. I just don't understand what exactly I have to do there, because this results in an error: 'getRefClass("Blabla")$accessors("a")' Can anyone point me to the correct use of 'getRefClass(.)$accessors(.)'? Thanks a lot, Janko Hi, fields can either be data or active binding functionss. See ?makeActiveBinding for some example of active binding functions in their original context. Here is an example of a reference class that keeps its data in a file. require( methods ) Foo <- setRefClass( "Foo", fields = list( file = "character", x = function(value){ if( missing( value ) ){ read.table( file ) } else{ write.table( value, file = file ) } } ) ) foo <- Foo$new( file="/tmp/iris.txt" ) foo$x <- iris foo$x write.table( subset( iris, Petal.Length < 2), file = "/tmp/iris.txt" ) foo$x Does that help ? Romain -- Romain Francois Professional R Enthusiast +33(0) 6 28 91 30 30 http://romainfrancois.blog.free.fr |- http://bit.ly/9VOd3l : ZAT! 2010 |- http://bit.ly/c6DzuX : Impressionnism with R `- http://bit.ly/czHPM7 : Rcpp Google tech talk on youtube __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Reference classes: accessor functions via 'getRefClass(...)$accessors(...)'
"Janko Thyson" writes: > Hi there, > > I'd like to choose between an "static" and "dynamic" access of a reference > class field, say 'a'. > > myObj <- getRefClass("Blabla")$new() > > Static: myObj$a > > Dynamic: myObj$a.get() where the function retrieves the data > from a database (or some other location), stores it to a buffer and > optionally updates the static field value 'a'. > > I've set up such a method 'a.get()' where I can actually decide between > static and dynamic runmode (so the method also wraps the call 'myObj$a' for > the static runmode). > > Now I saw that setting up such accessor methods (get/set) is already done > for me if I use 'getRefClass("Blabla")$accessors(.)'. I just don't > understand what exactly I have to do there, because this results in an > error: 'getRefClass("Blabla")$accessors("a")' > > Can anyone point me to the correct use of 'getRefClass(.)$accessors(.)'? It works for me: X <- setRefClass("XXX", fields = list(a = "character")) X$accessors("a") X # ... # Class Methods: #"callSuper", "export", "getA", "import", "initFields", "setA" # ... accessors() convenience method creates setA and getA functions: x <- X$new() x$getA #Class method definition for method getA() #function () #a # HTH, Vitalie. > > > Thanks a lot, > > Janko > > [[alternative HTML version deleted]] __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Reference classes: accessor functions via 'getRefClass(...)$accessors(...)'
Thanks a lot for the comments! I'll give the serveral alternatives a try to see if it gets me what I want. Cheers, Janko > -Ursprüngliche Nachricht- > Von: Vitalie S. [mailto:spinuvit.l...@gmail.com] > Gesendet: Mittwoch, 17. November 2010 13:51 > An: Janko Thyson > Cc: 'r-de...@r-project. org' > Betreff: Re: Reference classes: accessor functions via > 'getRefClass(...)$accessors(...)' > > "Janko Thyson" writes: > > > Hi there, > > > > I'd like to choose between an "static" and "dynamic" access of a > reference > > class field, say 'a'. > > > > myObj <- getRefClass("Blabla")$new() > > > > Static: myObj$a > > > > Dynamic: myObj$a.get() where the function retrieves the > data > > from a database (or some other location), stores it to a buffer and > > optionally updates the static field value 'a'. > > > > I've set up such a method 'a.get()' where I can actually decide > between > > static and dynamic runmode (so the method also wraps the call > 'myObj$a' for > > the static runmode). > > > > Now I saw that setting up such accessor methods (get/set) is already > done > > for me if I use 'getRefClass("Blabla")$accessors(.)'. I just don't > > understand what exactly I have to do there, because this results in > an > > error: 'getRefClass("Blabla")$accessors("a")' > > > > Can anyone point me to the correct use of > 'getRefClass(.)$accessors(.)'? > > It works for me: > > X <- setRefClass("XXX", fields = list(a = "character")) > X$accessors("a") > X > # ... > # Class Methods: > #"callSuper", "export", "getA", "import", "initFields", "setA" > # ... > > accessors() convenience method creates setA and getA functions: > > x <- X$new() > x$getA > #Class method definition for method getA() > #function () > #a > # > > HTH, > Vitalie. > > > > > > > Thanks a lot, > > > > Janko > > > > [[alternative HTML version deleted]] __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel