Here is a generic .First.lib()
function for loading settings
with package. It also (almost) assures that the package is detached
when R finishes. See onSessionExit
() why it is not guaranteed!
The almost generic .Last.lib()
function, which will prompt
user to save settings, is called when a package is detached.
It is custom to put these functions in a file named zzz.R
.
.First.lib():
.First.lib <- function(libname, pkgname) {
# Write a welcome message when package is loaded
pkg <- Package(pkgname)
assign(pkgname, pkg, pos=getPosition(pkg))# Read settings file ".<pkgname>Settings" and store it in package
# variable '<pkgname>Settings'.
varname <- paste(pkgname, "Settings")
basename <- paste(".", varname, sep="")
settings <- Settings$loadAnywhere(basename, verbose=TRUE)
if (is.null(settings))
settings <- Settings(basename)
assign(varname, settings, pos=getPosition(pkg))
# Detach package when R finishes, which will save package settings too.
onSessionExit(function(...) detachPackage(pkgname))
packageStartupMessage(getName(pkg), " v", getVersion(pkg),
" (", getDate(pkg), ") successfully loaded. See ?", pkgname,
" for help.\n", sep="")
} # .First.lib()
.Last.lib():
.Last.lib <- function(libpath) {
pkgname <- "<package name>"# Prompt and save package settings when package is detached.
varname <- paste(pkgname, "Settings", sep="")
if (exists(varname)) {
settings <- get(varname)
if (inherits(settings, "Settings"))
promptAndSave(settings)
}
} # .Last.lib()