reference-class

R reference classes as a field of a reference class

亡梦爱人 提交于 2019-12-06 14:25:50
问题 I would like to pass a reference class to a reference class constructor, and assign the passed reference class as a field. However, when I run the code below, I don't understand why I get an error. My questions are: 1) Please can someone kindly explain why this error is occurring: > a <- ClassA&new() Error in .getClassFromCache(Class, where) : argument "Class" is missing, with no default > b <- ClassB$new(a) Error in .Object$initialize(...) : object 'a' not found 2) I have declared class.a

Manual modifications of the class definition of a Reference Class instance

半世苍凉 提交于 2019-12-06 05:54:01
I'm aware that this would be a terribly unreliable hack. But out of pure interest: What would you need to manually change in the .refClassDef field of an ref class object if the Reference Class definition of an already instantiated object changed and you would like it to "be informed about the update" (without re-instantiating it). After all, it does seem to work if additional methods are introduced, but not for modifications of existing methods (see example below). This question is related to my answer in this post . Example Original class def: MyReferenceClass <- setRefClass(

Operator overloading for functions in R - strange behavior

对着背影说爱祢 提交于 2019-12-05 21:41:33
问题 Unfortunately things like (f+g)(3) where f and g are both unary functions do not work in R. Hence I tried to overload the "+" operator for unary functions in the following way: "+.function" = function(e1, e2){ return(function(x) e1(x) + e2(x)) } But if I try to use this, this does nothing. The code a = function(x) 2*x (a+a)(2) produces the same error as if +.function is not even defined. By some time playing around I found out that there is in fact a possibility to add functions in this way:

Inconsistency of S4 dispatch behavior for R6 classes

社会主义新天地 提交于 2019-12-05 16:11:52
Actual questions Shouldn't the fact that R6 classes inherit from (informal S3) class R6 allow the definition of S4 methods for signature arguments of that very class? As this is - AFAICT - not the case, what would be a workaround that is in line with current S3/S4 standards or that could somewhat be regarded as "best practice" in such situations? Background and example Reference Classes Consider the following example where you would like to define methods that dispatch on the superclass that all instances of Reference Classes inherit from ( envRefClass ): TestRefClass <- setRefClass(

Warnings thrown when accessing methods of reference class through RStudio

白昼怎懂夜的黑 提交于 2019-12-05 09:07:29
The code and the warnings: tinyclass <- setRefClass("TinyClass", methods = list(doNothing=function(){})) tc <- tinyclass() tc$doNothing() NULL Warning messages: 1: In installClassMethod(value, self, field, selfEnv, thisClass) : method .objectPackage from class TinyClass was not processed into a class method until being installed. Possible corruption of the methods in the class. 2: In installClassMethod(value, self, field, selfEnv, thisClass) : method .objectParent from class TinyClass was not processed into a class method until being installed. Possible corruption of the methods in the class.

R Reference Class issue

萝らか妹 提交于 2019-12-05 07:57:19
I am trying to create a simple reference class in R. Here is my code (R beginner): MyClass <- setRefClass("MyClass", fields = list(a = "numeric", b = "numeric"), methods = list( initialize <- function(){ print("Initializing") a <<- 1 b <<- 2 }, printValues <- function(){ print(a) print(b) } ) ) a <- MyClass$new() a$printValues() This produces the following error for the last line, a$printValues: Error in envRefInferField(x, what, getClass(class(x)), selfEnv) : "printValues" is not a valid field or method name for reference class “MyClass” Also, the initializer method is not being called ? Can

Method initialisation in R reference classes

こ雲淡風輕ζ 提交于 2019-12-05 00:46:54
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 code f_opt <- function(x) (t(x)%*%x) do_optim_opt <- function(x) optim(x,f) do_optim2_opt <- function(x)

Private Members in R Reference Class

余生颓废 提交于 2019-12-05 00:40:48
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 - amount > } > ) ) > > > tb <- Account$new(balance=50.75, number="baml-029873") tb$balance > tb$balance

R reference classes as a field of a reference class

爷,独闯天下 提交于 2019-12-04 20:10:28
I would like to pass a reference class to a reference class constructor, and assign the passed reference class as a field. However, when I run the code below, I don't understand why I get an error. My questions are: 1) Please can someone kindly explain why this error is occurring: > a <- ClassA&new() Error in .getClassFromCache(Class, where) : argument "Class" is missing, with no default > b <- ClassB$new(a) Error in .Object$initialize(...) : object 'a' not found 2) I have declared class.a.container as class "list", however I want this to be a reference class. What do I need to put here

how do I document an R Reference Class?

橙三吉。 提交于 2019-12-04 12:17:33
问题 how do I document the use of member functions of a reference class? if I write a Rd file with a \usage block, how do I avoid the WARNING Functions/methods with usage in documentation object 'XmlDoc' but not in code: $ new I'd expect the \usage block to allow me write things like: obj <- ClassName$new(par1, par2, ...) obj$method1(oth1, ...) and then I'd document the parameters in the \arguments block. If I do this, R CMD check complains with Assignments in \usage in documentation object