with_gc | R Documentation |
Logically, this function is equivalent to identity()
, simply
returning the value of the expression passed to it. However, it
also ensures that after evaluating this expression, a garbage
collection run is performed, even if the expression throws an
error.
with_gc(expr, ...)
expr |
The expression to evaluate. |
... |
Further arguments are passed to |
I have found this function occasionally useful when dealing with very large objects that bump up against the memory capacity of the computer I am using. One common use case is loading a very large R data file and keeping only one object from it while discarding the rest. However, overuse of this function when it is not needed will simply slow down your code.
gc()
with_gc({ # Create a "large" object (this one is not actually large because # examples need to be kept small) large_object <- 1:5000 # Return only a small piece of the object. large_object[5] }) # large_object has now been garbage-collected
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.