Witold E Wolski
2016-Jun-27 16:34 UTC
[R] refclasses question - Defining accessor function
Are accessors a fancy feature that do not work? I wanted to use accessor functions in a R refclass to hide the classes implementation where I am using sqlite. What I did observe is, that if I access in a method any of the fields (in the example below field .data in method printExample) all the accessor functions are called (even those not accessed by the function : in this case funnydata is not accessed). That means if any of the accessor functions is slow (in the example the funnydata accessor sleeps for 3 s) all the fields and all functions accessing any fields will be slow. In the example below accessing .data or calling printExample will take 3s. It's easy enough not to use accessor functions, so not a big deal. Still, I lost quite a bit of time wondering what is happening. Test <-setRefClass("Test", fields = list( .data = "list", funnydata = function(x){ Sys.sleep(3) if(missing(x)){ Sys.sleep(3) } } ), methods = list( initialise=function(){ .data <<- list(a="a")}, printExample = function(){ print("print Example") print(.data)} ) ) test<-Test() test$printExample() test$.data -- Witold Eryk Wolski