r6

R: Use active binding in object generator to conditionally add new class to R6 objects

半腔热情 提交于 2021-02-18 14:42:48
问题 I have a simple R6 object generator: thing <- R6Class("youngThing", private = list( ..age = 0), active = list( age = function(){ private$..age <- private$..age + 1 private$..age } ) ) That gives me a simple R6 object, where ..age increases by 1 every time the active age field is called: a_thing <- thing$new() a_thing$age # [1] 1 I want the object class of a_thing to change given a threshold value of the private field ..age , like this: class(a_thing) # [1] "youngThing" "R6" for(timestep in 1

R6Class - Encapsulation issue: Bad design?

自作多情 提交于 2021-02-07 10:49:17
问题 Minimal example Attaching R6 package require(R6) Element class definition element_factory <- R6Class( "Element", private = list( ..value = 0), active = list( value = function(new) { if(missing(new)) private$..value else private$..value <- new})) Container class definition container_factory <- R6Class( "Container", private = list( ..element = element_factory$new() ), active = list( element = function() private$..element)) Creating container istance co <- container_factory$new() Accessing

Use “with” with an R6 object

流过昼夜 提交于 2021-01-27 08:00:35
问题 I'm trying to use a construct such as "with" to allow easier (lazier ?) manipulation of an R6 object. Consider the following (minimal) example: library(R6) ToyClass<-R6Class(classname = "ToyClass", public = list( data=data.frame(x=c(1,2,3),y=c(4,5,6)), color = "red", symbol = 16, initialize=function(mult){ self$data$x <- self$data * mult } ) ) foo <- ToyClass$new(2) I can, obviously, plot the data in my object with plot(foo$data$x,foo$data$y,col=foo$color,pch=foo$symbol) However, being lazy

Use “with” with an R6 object

牧云@^-^@ 提交于 2021-01-27 07:57:22
问题 I'm trying to use a construct such as "with" to allow easier (lazier ?) manipulation of an R6 object. Consider the following (minimal) example: library(R6) ToyClass<-R6Class(classname = "ToyClass", public = list( data=data.frame(x=c(1,2,3),y=c(4,5,6)), color = "red", symbol = 16, initialize=function(mult){ self$data$x <- self$data * mult } ) ) foo <- ToyClass$new(2) I can, obviously, plot the data in my object with plot(foo$data$x,foo$data$y,col=foo$color,pch=foo$symbol) However, being lazy

Use “with” with an R6 object

久未见 提交于 2021-01-27 07:57:09
问题 I'm trying to use a construct such as "with" to allow easier (lazier ?) manipulation of an R6 object. Consider the following (minimal) example: library(R6) ToyClass<-R6Class(classname = "ToyClass", public = list( data=data.frame(x=c(1,2,3),y=c(4,5,6)), color = "red", symbol = 16, initialize=function(mult){ self$data$x <- self$data * mult } ) ) foo <- ToyClass$new(2) I can, obviously, plot the data in my object with plot(foo$data$x,foo$data$y,col=foo$color,pch=foo$symbol) However, being lazy

Use “with” with an R6 object

删除回忆录丶 提交于 2021-01-27 07:56:45
问题 I'm trying to use a construct such as "with" to allow easier (lazier ?) manipulation of an R6 object. Consider the following (minimal) example: library(R6) ToyClass<-R6Class(classname = "ToyClass", public = list( data=data.frame(x=c(1,2,3),y=c(4,5,6)), color = "red", symbol = 16, initialize=function(mult){ self$data$x <- self$data * mult } ) ) foo <- ToyClass$new(2) I can, obviously, plot the data in my object with plot(foo$data$x,foo$data$y,col=foo$color,pch=foo$symbol) However, being lazy

Proper way to implement S3 dispatch on R6 classes

流过昼夜 提交于 2020-01-23 04:53:10
问题 I have an R6 class and I want to add an S3 method for it. The documentation I found mentioned briefly that in order to use S3 dispatch on R6 you must have class = TRUE , but I couldn't find an example of how it should be done. I did see empirically that simply writing an S3 method in the form s3generic.r6class worked, but I wanted to know if that is indeed to right way to write an S3 method for R6 . For example, say I have an R6 class that enhances a list library(R6) R6list <- R6Class(

How to use `foreach` and `%dopar%` with an `R6` class in R?

末鹿安然 提交于 2020-01-02 07:51:15
问题 I ran into an issue trying to use %dopar% and foreach() together with an R6 class. Searching around, I could only find two resources related to this, an unanswered SO question and an open GitHub issue on the R6 repository. In one comment (i.e., GitHub issue) an workaround is suggested by reassigning the parent_env of the class as SomeClass$parent_env <- environment() . I would like to understand what exactly does environment() refer to when this expression (i.e., SomeClass$parent_env <-

Issue on using R6 classes and foreach() %dopar% together

我与影子孤独终老i 提交于 2019-12-23 22:52:50
问题 I'm having an issue on R6 classes when used with foreach() together, possibly to do with environments (I'm using Windows). Suppose that there are two R6 classes, "class1" and "class2". method1 in class1 is dependent on class2 (see example code below for example). The issue is, if I use foreach() %dopar% on class1, R doesn’t seem to recognise class2, even if I set .export = c("class1", "class2") explicitly in foreach() statement. (Here class1 uses class2) However if I use foreach() on class2,