December 2017
Beginner to intermediate
470 pages
12h 29m
English
Sometimes, it's useful to run a function when the object is garbage collected. If you're not familiar with garbage collection, you can think of it as a way to liberate unused memory when an object is no longer referenced by other objects in the environment.
A useful case for this feature is when you want to make sure a file or database connection gets closed before an object is garbage collected. To do this, you can define a finalize() method, which will be called with no arguments when the object is garbage collected. To test this functionality, you can simply add a finalizer as follows to some of your objects and see when you get the "Finalizer called" message in the console:
A <- R6Class("A", public = list( finalize = function() ...