I try to create dynamically the initialize function of R6 class. First I create unlocked class definition:
cls <- R6::R6Class(
name,
inherit=TPayload,
lock_objects=FALSE,
public=list(
module=class(tail(Parser$thrift_stack, 1)[[1]])[[1]],
ttype=ttype
))
And somewhere after I add an initialize function:
cls$set("public", 'initialize', init_func_generator(cls, default_spec))
where init_func_generator:
init_func_generator = function(cls, spec) {
func = function(...) {
cat('Hello\n')
}
return(func)
}
Executing simple initialize function works when I create an object.
cli$new(name='abc')
However when I try to access 'spec' variable from outside scope of created 'initialize':
init_func_generator = function(cls, spec) {
func = function(...) {
for(s in spec) {
cat(str(s))
}
}
return(func)
}
I get:
* object 'spec' not found
Is it possible what I try to achieve?