Question

I'm trying to set the params field and validate it in the Template class, so I can do TemplateClass$new(params) and it automatically validates, but I get an error:

Template <- setRefClass('Template',
    fields = c(
       "params"
    ),

    methods = list(
        initialize = function(params){
            params <<- params
            validate_params()
        },
        validate_params = function(){
            "everything okay"
        }
    )
)

PointsTemplate <- setRefClass('PointsTemplate',

    contains = "Template",

    methods = list(
        initialize = function(params){
            callSuper(params)
        }
    )
)

Error in .Object$initialize(...) :
  argument "params" is missing, with no default

EDIT: I seemed to have solved it by changing the initialize method in the Template class to initialize = function(params = NULL){}. But I don't understand why this is needed.

Also, I've seen other people use callSuper() when the class doesn't contain any super classes. What is the reason for this?

Was it helpful?

Solution

I don't have any experience with R reference classes but using the help of ?ReferenceClasses you can read the following:

Initialization methods need some care in design, as they do for S4 classes. Therefore, your method should normally include ... as an argument, all other arguments should have defaults or check for missingness, and your method should pass all initialized values on via $callSuper() or $initFields() if you know that your superclasses have no initialization methods.

So I understand this by setting my initialization function in the parent class using ... and named parameters for all fields with a default values.

Template <- setRefClass('Template',
                        fields = list(params="list"),
                        methods = list(initialize = 
                            function(...,params=list(1:5)){
                              callSuper(...,params=params)
                              validate_params()
                            },
                          validate_params = function(){
                            "everything okay"
                          }
                        )
)

For the sub-Class no need to initialize parameters since I am sure that super class will dot it.

PointsTemplate <- setRefClass('PointsTemplate',
                              contains = "Template",
                              methods = list(initialize = 
                                 function(...){
                                  callSuper(...)
                                }

                             ))

No testing the initialization:

## using default values
> PointsTemplate$new()
Reference class object of class "PointsTemplate"
Field "params":
[[1]]
[1] 1 2 3 4 5
## setting params 
 PointsTemplate$new(params=list(1:10))
Reference class object of class "PointsTemplate"
Field "params":
[[1]]
 [1]  1  2  3  4  5  6  7  8  9 10
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top