reference-class

Auto update of a field (data member) in R reference class

大城市里の小女人 提交于 2020-01-14 03:30:06
问题 Any equivalent as the decorator @property in python? That is to have one field automatically updated (on access) due to an update of another field, instead of recomputing it before access. UPDATE 2014-02-06 By defining "one" field as an activeBindingFunction of "another" field turns on the automatic updates (See @jdharrison's answer). However, is there anyway to check whether such updates are lazy evaluation? If not, how can we achieve this? (A later edit: an "informal" print function inside

Speeding up field access in R reference classes

时光总嘲笑我的痴心妄想 提交于 2019-12-23 12:08:04
问题 I have been writing code using R reference classes. However, as I have progressed, the program has become intolerably slow. To demonstrate the problem, take the following example: myClass <- setRefClass( "Class"="myClass", fields=c( counter="numeric" ), methods=list( initialize=function () { counter <<- 0 }, donothing=function () { }, rand=function () { return(runif(1)) }, increment=function () { counter <<- counter + 1 } ) ) mc <- myClass() system.time(for (it in 1:500000) { mc$donothing() }

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

半城伤御伤魂 提交于 2019-12-23 02:42:05
问题 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) } ) )

Inconsistency of S4 dispatch behavior for R6 classes

邮差的信 提交于 2019-12-22 09:23:20
问题 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

Warnings thrown when accessing methods of reference class through RStudio

半世苍凉 提交于 2019-12-22 06:49:13
问题 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

R Reference Class issue

ⅰ亾dé卋堺 提交于 2019-12-22 05:24:11
问题 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

Roxygen2 - how to @export reference class generator?

懵懂的女人 提交于 2019-12-22 04:01:52
问题 For instance, say I have the following package called Test and I want to export class A : # In /R/Test.R: #' @docType package #' @import methods #' @exportClass A A <- setRefClass("A", methods = list(foo = identity)) However, after building and loading, I get the following error when using A 's generator: > library(Test) > A()$foo(1) Error: could not find function "A" I've checked the contents of my NAMESPACE file is fine: exportClasses(A) import(methods) So what's going wrong? Why isn't my

Reference Class with custom field classes in R?

核能气质少年 提交于 2019-12-20 14:41:49
问题 I would like to use a custom reference class inside another reference class, but this code fails: nameClass <- setRefClass("nameClass", fields = list(first = "character", last = "character"), methods = list( initialize = function(char){ chunks <- strsplit(char,"\\.") first <<- chunks[[1]][1] last <<- chunks[[1]][2] }, show = function(){ cat("Special Name Class \n:") cat("First Name:") methods::show(first) cat("Last Name:") methods::show(last) } )) # this works fine nameClass$new("tyler.durden

Error in initFields(scales = scales) : could not find function “initRefFields”

拈花ヽ惹草 提交于 2019-12-20 02:50:16
问题 I have a ggplot2 plotting function as part of my code. The function works fine when the file is sourced as R code, however when I include this function in an R package (and of course I include ggplot2 and scales both in the DESCRIPTION and in the NAMESPACE files of the package) I am getting the following error: Error in initFields(scales = scales) : could not find function "initRefFields" The respective call of scales in the ggplot2 object is the following: + facet_wrap(~PV_Type, ncol = 1,

`print` method for ReferenceClass

我的梦境 提交于 2019-12-19 04:12:59
问题 I have: MyClass <- setRefClass("MyClass" , fields = list(data="numeric")) Let's initialize an object of MyClass : OBJ <- MyClass(data=1:4) ... and print it on screen: OBJ Reference class object of class "MyClass" Field "data": [1] 1 2 3 4 I would like to change the way it gets printed so I wrote this method: print.MyClass <- function(x) { cat("This is printed representation: ") print(x$data) } Now this works: print(OBJ) This is printed representation: [1] 1 2 3 4 this doesn't: OBJ Is there