initializeOptionContainer: Initialize Option Container (generic)

Description Usage Arguments Author(s) References See Also Examples

Description

Convenience function to initialize the option container that is then stored as an R option according to the name/ID provided by id.

Usage

1
2
3
4
initializeOptionContainer(id = tryCatch(devtools::as.package(".")$package,
  error = function(cond) {     stop("Invalid default value for `id`") }),
  sub_id = character(), components = c("options", ".meta", ".registry"),
  check = TRUE, hidden = TRUE, overwrite = FALSE, ...)

Arguments

id

Signature argument. Object containing suitable information to control the distinct creation process. In the simplest case, this corresponds to the name/ID of a package/package project. But it can also be an instance of a class for which methods for initializeProjectOptions, initializeMeta and initializeRegistry exist.

sub_id

character. Optional ID for a sub layer. Useful for a hub-like option container structure.

check

logical. TRUE: check if an R option with name/ID according to the information in id already exists (in which case an error is thrown); FALSE: no check for existing R options. Note that overwrite will overrule check.

hidden

logical. TRUE: make sure name/ID information in id is preprended with a dot to hide it; FALSE: use name/ID information in id as is. The former reduces the risk of accidentially overwriting existing R options and thus is used by default.

overwrite

logical. TRUE: overwrite existing container; FALSE: create only if no container exists yet.

...

Further arguments to be passed to subsequent functions/methods.

Author(s)

Janko Thyson janko.thyson@gmail.com

References

http://github.com/Rappster/optionr

See Also

initializeOptionContainer-char-method, ensureOptionContainer, getOptionContainer

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
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
## Not run: 

##------------------------------------------------------------------------------
## Default ID //
##------------------------------------------------------------------------------
  
## Make sure initial options are `NULL` //
options("optionr" = NULL)

id_hidden <- paste0(".", devtools::as.package(".")$package)
initializeOptionContainer()
opts <- getOption(id_hidden)
opts$options  
opts$.meta  
opts$.registry  

## Clean up //
options(id_hidden = NULL)

##------------------------------------------------------------------------------
## Explicit ID //
##------------------------------------------------------------------------------
  
id <- "test"
id_hidden <- paste0(".", id)
options(id = NULL)

initializeOptionContainer(id)
opts <- getOption(id_hidden)
opts$options  
opts$.meta  
opts$.registry

## Clean up //
options(id_hidden = NULL)

##------------------------------------------------------------------------------
## Sub ID //
##------------------------------------------------------------------------------
  
id <- "test"
id_hidden <- paste0(".", id)
sub_id <- "a"
options(id = NULL)

initializeOptionContainer(id = id, sub_id = sub_id)
opts <- getOption(id_hidden)
"a" %in% ls(opts)
opts[[sub_id]]$options  
opts[[sub_id]]$.meta  
opts[[sub_id]]$.registry

## Clean up //
options(id_hidden = NULL)
  
##------------------------------------------------------------------------------
## Partial 
##------------------------------------------------------------------------------

id <- "test"
id_hidden <- paste0(".", id)
options(id_hidden = NULL)
initializeOptionContainer(id, components = "options")
opts <- getOption(id_hidden)
opts$options  
opts$.meta
## --> not created
opts$.registry
## --> not created

initializeOptionContainer(id, components = c("options", ".meta"), 
  overwrite = TRUE)
opts <- getOption(id_hidden)
opts$options  
opts$.meta
opts$.registry
## --> not created

## Condition handling //
initializeOptionContainer(id, components = c("nonexisting", "options"), 
  overwrite = TRUE)
opts <- getOption(id_hidden)
ls(opts, all.names = TRUE)

## Clean up //
options(id_hidden = NULL)
  
##------------------------------------------------------------------------------
## As interface //
##------------------------------------------------------------------------------

## Example of how custom classes can be used for creating custom methods //
id <- structure(list(id = "test"), class = "OptionContext.Test")
id
id_hidden <- paste0(".", id$id)

options(id_hidden = NULL)
initializeOptionContainer(id)
## --> calls `initializeProjectOptions()`, `initializeMeta()` and `initializeRegistry()`
## and for each of which methods for signature ID can be defined

getOption(id_hidden)$options  
getOption(id_hidden)$.meta  
getOption(id_hidden)$.registry  
options(id_hidden = NULL)


## End(Not run)

rappster/optionr documentation built on May 26, 2019, 11:23 p.m.