Learn R Programming

gtools (version 3.9.5)

lastAdd: Non-destructively construct a .Last function to be executed when R exits.

Description

Non-destructively construct a .Last function to be executed when R exits.

Usage

lastAdd(fun)

Value

A new function to be used for .Last.

Arguments

fun

Function to be called.

Author

Gregory R. Warnes greg@warnes.net

Details

lastAdd constructs a new function which can be used to replace the existing definition of .Last, which will be executed when R terminates normally.

If a .Last function already exists in the global environment, the original definition is stored in a private environment, and the new function is defined to call the function fun and then to call the previous (stored) definition of .Last.

If no .Last function exists in the global environment, lastAdd simply returns the function fun.

See Also

Examples

Run this code


## Print a couple of cute messages when R exits.
helloWorld <- function() cat("\nHello World!\n")
byeWorld <- function() cat("\nGoodbye World!\n")

.Last <- lastAdd(byeWorld)
.Last <- lastAdd(helloWorld)
# \dontshow{
.Last()
# }
if (FALSE) {
q("no")

## Should yield:
##
##   Save workspace image? [y/n/c]: n
##
##   Hello World!
##
##   Goodbye World!
##
##   Process R finished at Tue Nov 22 10:28:55 2005
}

Run the code above in your browser using DataLab