Settings

0th

Percentile

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.

Keywords
classes, programming, IO
Usage
Settings(basename=NULL, ...)
Arguments
basename

A character string of the basename of the settings file.

...

Arguments passed to constructor of superclass Options.

Fields and Methods

Methods:

findSettings Searches for the settings file in one or several directories.
getLoadedPathname Gets the pathname of the settings file loaded.
isModified Checks if settings has been modified compared to whats on file.
loadAnywhere Loads settings from file.
promptAndSave Prompt user to save modified settings.
saveAnywhere Saves settings to file.

Methods inherited from Options: as.character, as.list, equals, getLeaves, getOption, hasOption, names, nbrOfOptions, setOption, str

Methods inherited from Object: $, $<-, [[, [[<-, as.character, attach, attachLocally, clearCache, clearLookupCache, clone, detach, equals, extend, finalize, getEnvironment, getFieldModifier, getFieldModifiers, getFields, getInstantiationTime, getStaticInstance, hasField, hashCode, ll, load, names, objectSize, print, save

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

Aliases
  • Settings
Examples
# NOT RUN {
# 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
file.remove(getLoadedPathname(settings2))

# Assert correctness
stopifnot(equals(settings, settings2))

# }
Documentation reproduced from package R.utils, version 2.8.0, License: LGPL (>= 2.1)

Community examples

Looks like there are no examples yet.