You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The idea is that sometimes I have a class that keeps a pointer to a C++ object, for example:
myClass <- R6::R6Class(
"myClass",
public = list(
xp = NULL,
initialize = function(x) {
self$xp = cpp_fun_that_returns_a_pointer(x)
}
)
)
When cloning this class (even with deep=TRUE) the c++ object won't be cloned.
So it would be nice to be able to override the clone method behavior. Is it possible?
Maybe allow it only when cloneable = FALSE.
The text was updated successfully, but these errors were encountered:
This seems to be already possible. Quoting here the answer from @duckmayr on SO.
# Set up the R6 class, making sure to set cloneable to FALSE
myClass <- R6::R6Class(
"myClass",
public = list(
xp = NULL,
initialize = function(x = 1:3) {
self$xp = x
}
),
cloneable = FALSE
)
# Set the clone method
myClass$set("public", "clone", function() {
print("This is a custom clone method!") # Test print statement
myClass$new(self$xp)
})
# Make a new myClass object
a <- myClass$new(x = 4:6)
# Examine it
a
#> <myClass>
#> Public:
#> clone: function ()
#> initialize: function (x = 1:3)
#> xp: 4 5 6
# Clone it
b <- a$clone()
#> [1] "This is a custom clone method!"
# We see the test print statement was printed!
# Let's check out b:
b
#> <myClass>
#> Public:
#> clone: function ()
#> initialize: function (x = 1:3)
#> xp: 4 5 6
Uh oh!
There was an error while loading. Please reload this page.
I opened an SO post here.
The idea is that sometimes I have a class that keeps a pointer to a C++ object, for example:
When cloning this class (even with
deep=TRUE
) the c++ object won't be cloned.So it would be nice to be able to override the
clone
method behavior. Is it possible?Maybe allow it only when
cloneable = FALSE
.The text was updated successfully, but these errors were encountered: