Class for applicational settings

Package: R.utils Class Settings Object ~~| ~~+--Options ~~~~~~~| ~~~~~~~+--Settings Directly known subclasses: public static class Settings extends Options Class for applicational settings.

classes, programming, IO
Settings(basename=NULL, ...)
A character string of the basename of the settings file.
Arguments passed to constructor of superclass Options.
Load settings with package and save on exit

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 ".Settings" and store it in package # variable '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)); cat(getName(pkg), " v", getVersion(pkg), " (", getDate(pkg), ")", " successfully loaded. See ?", pkgname, " for help.\n", sep=""); } # .First.lib() .Last.lib(): .Last.lib <- function(libpath) { pkgname <- ""; # 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()

  • Settings
# Load settings from file, or create default settings
basename <- "some.settings"
settings <- Settings$loadAnywhere(basename)
if (is.null(settings))
  settings <- Settings(basename)

# Set default options, if missing.
setOption(settings, "graphics/verbose", TRUE, overwrite=FALSE)
setOption(settings, "io/verbose", Verbose(threshold=-1), overwrite=FALSE)

# Save and reload settings
path <- tempdir()
saveAnywhere(settings, path=path)
settings2 <- Settings$loadAnywhere(basename, paths=path)

# Clean up

# Assert correctness
stopifnot(equals(settings, settings2))
Documentation reproduced from package R.utils, version 0.5.5, License: LGPL version 2.1 or newer (the releases)

Community examples

Looks like there are no examples yet.