“User” constructor for R6 class in R

前端 未结 1 1208
北恋
北恋 2021-01-23 03:40

I am learning how to use R6 classes (and in general R OO).

In this tutorial I found an interesting way of presenting constructors. In section 6.3 a different kind of con

相关标签:
1条回答
  • 2021-01-23 04:16

    Since R6 classes are actually envoronments, you can use className$constructorName to archieve this result.

    library(R6)
    
    ERes <- R6Class(
      "ERes",
      public = list(
        eTable = NULL,
        eList = NULL,
        initialize = function(eTable, eList){
          self$eTable <- eTable
          self$eList <- eList
        }
      )
    )
    
    ERes$userConstructor <- function(someData){
      ERes$new(table(someData), as.list(someData))
    }
    
    myObject <- ERes$userConstructor(rpois(100, 5))
    
    myObject$eTable
    # someData
    #  0  1  2  3  4  5  6  7  8 10 
    #  3  3  7 16 16 20 14 10  9  2 
    
    0 讨论(0)
提交回复
热议问题