1

Taking an example of a basic R6 class given below:

library(R6)

Demo = R6Class("Demo",
           public = list(
             initialize = function(){
               message("demo object initialized!")
             },
             delete = function(){
               #should contain the logic to delete the object
               gc()
             },
             finalize = function(){
               message("demo object deleted!")
             } 
           ))

demoObj = Demo$new()
demoObj$delete()

What should be the logic inside the delete function? Is it possible to delete the object (self)?

Akshit
  • 424
  • 4
  • 15
  • I'm not familiar with r. But, with other OO languages (e.g. Java, Ruby), gc is done once there are no more references to the object. In other words, the object does not delete itself. It is garbage collected once it's no longer being used. – aridlehoover Aug 08 '20 at 05:23
  • Thanks, I understand the use of gc but wanted to know if there is any way to remove the object itself with something like rm(obj). – Akshit Aug 08 '20 at 10:05

1 Answers1

2

I'm not aware of any straightforward way of getting an object in R to "self-destruct" in this way. The object belongs to the environment in which it was created, and there are good reasons for leaving control over the lifetime of an object to the environment in which it was created, rather than to the object itself.

This doesn't mean there is no way to do it, but effectively you would have to allow the object to look up its own name in its parent frame (using a flag that identifies it only once the deletion process is set in motion), then calling rm on that name in the parent frame. So something like this reprex:

library(R6)

Demo = R6Class("Demo",
           public = list(
             initialize = function(){
               message("demo object initialized!")
             },
             delete_flag = FALSE,
             delete = function(){
               self$delete_flag <- TRUE
               pf <- parent.frame()
               demos <- sapply(ls(pf), function(i) {
                          class(get(i, envir = pf))[1] == "Demo"
                        })
               this <- ls(pf)[demos][sapply(mget(ls(pf)[demos], envir = pf),
                      function(x) x$delete_flag)]
               rm(list = this, envir = pf)
               message("demo object deleted!")
             }
           ))

demoObj = Demo$new()
#> demo object initialized!

ls()
#> [1] "Demo"    "demoObj"

demoObj$delete()
#> demo object deleted!

ls()
#> [1] "Demo"

This is designed to ensure that only the correct object is deleted, so for example;

demoObj1 = Demo$new()
#> demo object initialized!
demoObj2 = Demo$new()
#> demo object initialized!
demoObj3 = Demo$new()
#> demo object initialized!

demoObj2$delete()
#> demo object deleted!

ls()
#> [1] "Demo"     "demoObj1" "demoObj3"

You can see that only demoObj2 has deleted itself.

Created on 2020-08-08 by the reprex package (v0.3.0)

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • Would it make sense to make delete_flag private as it's just for internal use? – Akshit Aug 09 '20 at 02:22
  • 1
    @AkshitAchara yes, although you would then need a getter function so that it can be read. – Allan Cameron Aug 09 '20 at 07:22
  • thanks, this method deletes the object from the parent environment. Is there any way to look for all the references of the object and delete them i.e if another R6 object contains the object that we wish to delete, then the object should be deleted from there also. (If `demos <- c(demoObj1, demoObj2)`, then `demoObj1$delete()` should remove it from demos also) – Akshit Aug 11 '20 at 17:41
  • 1
    @Akshit no, there are no easy ways to do this, for the simple reason that an object does not have any information about what points at it and what names it has been given. Like I said, it's a real stretch to rely on an object's own `delete` method to try to get rid of it. Please believe me: whatever it is you're trying to do, there is a better way to do it. Think about a different design, and if you get stuck there are lots of people here who can help. – Allan Cameron Aug 11 '20 at 17:57