Registers an R function to be called upon garbage collection of object or (optionally) at the end of an R session.
reg.finalizer(e, f, onexit = FALSE)
| Parameter | Description |
|---|---|
e |
Object to finalize. Must be an environment or an external pointer. |
f |
Function to call on finalization. Must accept a single argument, which will be the object to finalize. |
onexit |
logical: should the finalizer be run if the object is still uncollected at the end of the R session? |
# NOT RUN {
f <- function(e) print("cleaning....")
g <- function(x){ e <- environment(); reg.finalizer(e, f) }
g()
invisible(gc()) # trigger cleanup
# }