How to avoid prepending .self when using eval in a reference class in R? -


i need use eval call reference class method. below toy example:

myclass <- setrefclass("myclass",      fields = c("my_field"),      methods = list(          initialize = function(){             my_field <<- 3         },          hello = function(){             "hello"         },          run = function(user_defined_text){             eval(parse(text = user_defined_text))         }     ) )  p <- myclass$new() p$run("hello()") # error: not find function "hello" - doesn't work p$run(".self$hello()") # "hello" - works p$run("hello()") # "hello" - works?!  p <- myclass$new() p$run("my_field") # 3 - no need add .self 

i guess eval(parse(text = paste0(".self$", user_defined_text))), don't understand:

  • why .self needed eval methods, not fields?
  • why .self no longer needed after has been used once?

'why' questions challenging answer; answer 'because'. on ?setrefclass have

only methods used included in environment corresponding individual object.  declare method requires particular other method, first method should include call '$usingmethods()' name of other method argument. declaring methods way essential if other method used indirectly (e.g., via 'sapply()' or 'do.call()'). if called directly, code analysis find it. declaring method harmless in case, however, , may aid readability of source code. 

i'm not sure entirely helpful in case, user apparently able specify method. offering little unasked editorial comment, i'm not sure 'why' you'd want write method parse input text methods; i've never used paradigm myself.


Comments

Popular posts from this blog

ios - Memory not freeing up after popping viewcontroller using ARC -

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -