Closed dselivanov closed 7 years ago
@wch If this involves deep digging, can you suggest some workarounds?
The reason it happens is because the clone
method makes this assumption: any members of the R6 object that are functions are also methods on the object, and it will reassign the environment of the function after it copies it to the new object. I'm not sure whether this is behavior that I'd want to keep or not -- there are some pros and cons to it.
One thing you could do for now is simply wrap the function in a list, so that clone
won't think it's a method and reassign the environment. So you'd have something like this:
fun_caller = R6::R6Class(
public = list(
initialize = function(f_) self$f = list(f_),
f = list(),
call_f = function(x) self$f[[1]](x)
)
)
First of all many thanks for the huge amount of work done by
R6
developers and contributors. I useR6
v2.1.3. Recently I discovered a strange behaviour ofR6
classes (looks like a bug) - when instance of the class is cloned, it can't correctly capture namespace/environment of the member function. Description above can looks unclear, so it will be simpler to consider following example (my use case a little bit more complex - I useR6
to create iterators which can apply some function to the chunk after$next()
call):check_input
- is a internal function intokenizers
package and it is not exported!However this works fine:
And this also works: