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

Share:

Description

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

Usage

1
lastAdd(fun)

Arguments

fun

Function to be called.

Details

lastAdd constructs a new function which can be used to replace the exising 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.

Value

A new function to be used for .Last.

Note

This function replaces the (now defunct) addLast function.

Author(s)

Gregory R. Warnes greg@warnes.net

See Also

.Last

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
## 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)


## Not run: 
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

## End(Not run)

## 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")
}
.Last <- lastAdd(myLast)

## Not run: 
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

## End(Not run)

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.