Learn R Programming

fame (version 1.13)

addLast: Add a function to be executed when R exits.

Description

Add a function to be executed when R exits.

Usage

addLast(fun)

Arguments

fun
Function to be called.

Value

  • None.

Details

addLast defines .Last (if not already present) or redefines it so that the function fun will be called when R exits. The latter is accomplished by saving the current definition of .Last and creating a new .Last function that calls fun and then the original .Last function.

See Also

.Last

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")

addLast(byeWorld)
addLast(helloWorld)

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

## Unix-flavour example: send Rplots.ps to printer on exit.
myLast <- function()
{
  cat("Now sending PostScript graphics to the printer:\n")
  system("lpr Rplots.ps")
  cat("bye bye...\n")
}
addLast(myLast)
quit("yes")

## Should yield:
##
##  Now sending PostScript graphics to the printer:
##  lpr: job 1341 queued
##  bye bye...
##
##   Process R finished at Tue Nov 22 10:28:55 2005

Run the code above in your browser using DataLab