且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

自动更新R参考类中的字段(数据成员)

更新时间:2023-02-08 17:34:54

不确定它是否在找你

test <- setRefClass("test",
                            fields   = list(
                                            num1 = "numeric"
                                            , num2 = function(){
                                                                print("hello")
                                                                 2*.self$num1
                                                                }
                                           ),
                            methods  = list(
                              initialize = function(num1 = 1){
                                num1 <<- num1
                              },

                              show = function(){
                                print(c(num1 = num1, num2 = num2))
                              }
                            )
)

> tt <- test()
> tt
[1] "hello;"
num1 num2 
   1    2 
> tt$num1<-4
> tt
[1] "hello;"
num1 num2 
   4    8