Consider the following R code :
MyClass <- setRefClass("MyClass",
fields = list(
),
methods = list(
initialize = function(x){
#give new attribute x
}
)
)
my_var_name <- 1
obj <- MyClass$new(x = my_var_name)
I would like to add an attribute to MyClass
. The name of the attribute is my_var_name
, as specified by the user input. The value of the attribute, as will need to be accessed by obj$my_var_name
, should be equal to the value of the variable, which in this case is 1
. If such a thing is difficult to do in general, I would like it done at least in the constructor. The class
of the attribute, which in this case is numeric
, can be seen as known.
I know that deparse(substitute())
can be used to extract the variable name. But how do I define an attribute using this ?