reference-class

Reference Classes, tab completion and forced method definition

混江龙づ霸主 提交于 2019-12-18 16:16:29
问题 I am currently writing a package using reference classes. I have come across an issue which from reading various sources: Method initialisation in R reference classes Can't reliably use RefClass methods in Snowfall I gather is caused because reference methods are not all copied to every object in the class rather they are copied when first accessed. https://stat.ethz.ch/pipermail/r-devel/2011-June/061261.html As an example define: test <- setRefClass("TEST", fields = list( a = "numeric"),

Dynamically Generate Reference Classes

落花浮王杯 提交于 2019-12-18 14:49:39
问题 I'm attempting to generate reference classes within an R package on the fly, and it's proving to be fairly difficult. Here are the approaches I've taken and problems I've run into: I'm creating a package in which I hope to be able to dynamically read in a schema and automatically generate an associated reference class (think SOAP). Of course, this means I won't be able to define my reference classes before-hand in the package sources. I initially attempted to create a new class using a simple

What is the significance of the new Reference Classes?

不打扰是莪最后的温柔 提交于 2019-12-17 06:28:57
问题 Apparently John Chambers added Reference Classes to R in version 2.12. There doesn't appear to be much information online yet, but they're calling them R5 classes, which implies they're on a level with S3 and S4 classes. Question: What is a reference class, and how does this fit in with existing class types? 回答1: The request for documentation for ReferenceClasses comes up every now and then, for example on the r-devel list. The best answer so far is to actually look at what help

Return a list of fields of a Reference Class

旧巷老猫 提交于 2019-12-13 04:22:25
问题 What is the proper way to define a function which returns a arbitrary number of fields. A.getFields<-function(values){ vars<-getRefClass()$fields() idx<-names(vars) %in% values if(length(fields)<1) return(vars) return(vars[idx]) } A.setFields<-function(...){ dots <- list(...) fieldNames <- names(dots) for(i in seq_along(dots)) assign(fieldNames[[i]], dots[[i]], attr(.self, ".xData")) } A<-setRefClass(Class = "A", fields = c(var1 = "list", var2="character"), methods = list(getFields=A

how to extend a reference class defined in an R package?

限于喜欢 提交于 2019-12-12 17:30:08
问题 I want to allow users to extend a reference class I define in my package. Here is a toy example: # my_package/R/Main.R #' My Main class #' @export Main <- setRefClass("Main") After loading this package, I get a warning when I try to extend it: library(my_package) Child <- setRefClass("Child", contains = "Main") # Warning message: # Class "Main" is defined (with package slot ‘my_package’) but no metadata object found to revise subclass information---not exported? Making a copy in package ‘

Method initialisation in R reference classes

醉酒当歌 提交于 2019-12-10 02:21:05
问题 I've noticed some strange behaviour in R reference classes when trying to implement some optimisation algorithm. There seems to be some behind-the-scenes parsing magic involved in initialising methods in a particular which makes it difficult to work with anonymous functions. Here's an example that illustrates the difficulty: I define a function to optimise (f_opt), a function that runs optim on it, and a reference class that has these two as methods. The odd behaviour will be clearer in the

R: Change the fields' (slots') values of the class assigning a value to some other field

不想你离开。 提交于 2019-12-08 09:39:20
问题 Assgning a value to one field, how can I make the other fields changing. Consider the following ReferenceClass object: C<-setRefClass("C", fields=list(a="numeric",b="numeric") , methods=list( seta = function(x){ a<<-x b<<-x+10 cat("The change took place!") } ) # end of the methods list ) # end of the class Now create the instance of the class c<-C$new() This command c$seta(10) will result in that c$a is 10 and c$b is 20. So it actually works, however, I want to achieve this result by the

How to tell an R reference class object to update its method definitions?

≯℡__Kan透↙ 提交于 2019-12-08 07:22:48
问题 I'm looking for a way to tell an instance of a reference class to forget one of its method definitions. For example, I create the class MyReferenceClass and an instance called my_object I can call the method print_hello and everything works: MyReferenceClass <- setRefClass("MyReferenceClass", methods = list( print_hello = function(){ print("hello") } ) ) my_object <- MyReferenceClass$new() my_object$print_hello() # "hello" If I update the class definition by adding a new method ( print

R Reference Class multiple inheritance: how to call method in a specific parent class?

空扰寡人 提交于 2019-12-06 21:16:27
I have a reference class Child which inherits from parents SuperA and SuperB . When in the initialize method of Child , I would like to invoke the initialize methods of both SuperA and SuperB in turn. Thus, for instance, I have: SuperA <- setRefClass("SuperA", fields = list(a = "ANY"), methods = list( initialize = function(a) { print(a) initFields(a = a) } ) ) SuperB <- setRefClass("SuperB", fields = list(b = "ANY"), methods = list( initialize = function(b) { print(b) initFields(b = b) } ) ) Child <- setRefClass("Child", contains = c("SuperA", "SuperB"), methods = list( initialize = function(a

Private Members in R Reference Class

限于喜欢 提交于 2019-12-06 19:28:26
问题 Is it possible to have private member fields inside of an R reference class. Playing with some of the online examples I have: > Account <- setRefClass( "ref_Account" > , fields = list( > number = "character" > , balance ="numeric") > , methods = list( > deposit <- function(amount) { > if(amount < 0) { > stop("deposits must be positive") > } > balance <<- balance + amount > } > , withdraw <- function(amount) { > if(amount < 0) { > stop("withdrawls must be positive") > } > balance <<- balance -