/
readtext_options.R
114 lines (104 loc) · 3.54 KB
/
readtext_options.R
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
109
110
111
112
113
114
#' Get or set package options for readtext
#'
#' Get or set global options affecting functions across \pkg{readtext}.
#' @param ... options to be set, as key-value pair, same as
#' [options()]. This may be a list of valid key-value pairs, useful
#' for setting a group of options at once (see examples).
#' @param reset logical; if `TRUE`, reset all \pkg{readtext} options to
#' their default values
#' @param initialize logical; if `TRUE`, reset only the \pkg{readtext}
#' options that are not already defined. Used for setting initial values when
#' some have been defined previously, such as in `.Rprofile`.
#' @details Currently available options are: \describe{
#' \item{`verbosity`}{Default
#' verbosity for messages produced when reading files. See
#' [readtext()].}
#' }
#' @return When called using a `key = value` pair (where `key` can be
#' a label or quoted character name)), the option is set and `TRUE` is
#' returned invisibly.
#'
#' When called with no arguments, a named list of the package options is
#' returned.
#'
#' When called with `reset = TRUE` as an argument, all arguments are
#' options are reset to their default values, and `TRUE` is returned
#' invisibly.
#' @export
#' @examples
#' \dontrun{
#' # save the current options
#' (opt <- readtext_options())
#'
#' # set higher verbosity
#' readtext_options(verbosity = 3)
#'
#' # read something in here
#' if (!interactive()) pkgload::load_all()
#' DATA_DIR <- system.file("extdata/", package = "readtext")
#' readtext(paste0(DATA_DIR, "/txt/UDHR/*"))
#'
#' # reset to saved options
#' readtext_options(opt)
#' }
readtext_options <- function(..., reset = FALSE, initialize = FALSE) {
args <- list(...)
# if the ... is a list already, use that
if (length(args) == 1 && is.list(args[[1]]))
args <- args[[1]]
# initialize automatically it not yet done so
if (is.null(options("readtext_initialized")) || !"package:readtext" %in% search())
readtext_initialize()
if (initialize) {
readtext_initialize()
return(invisible(TRUE))
} else if (reset) {
readtext_reset()
return(invisible(TRUE))
} else if (!length(args)) {
# return all option values with names
opts_names <- names(get_options_default())
opts <- options()[paste0("readtext_", opts_names)]
names(opts) <- stri_sub(names(opts), 10, -1) # remove prefix
return(opts)
} else if (is.null(names(args))) {
# return a option value
return(getOption(paste0("readtext_", args[[1]])))
} else {
# set value
for (key in names(args)) {
set_option_value(key, args[[key]])
}
return(invisible(args))
}
}
readtext_initialize <- function() {
opts <- get_options_default()
for (key in names(opts)) {
if (is.null(getOption(paste0("readtext_", key))))
set_option_value(key, opts[[key]])
}
options("readtext_initialized" = TRUE)
}
readtext_reset <- function() {
opts <- get_options_default()
for (key in names(opts)) {
set_option_value(key, opts[[key]])
}
options('readtext_initialized' = TRUE)
}
set_option_value <- function(key, value) {
opts <- get_options_default()
# check for key validity
if (!key %in% names(opts))
stop(key, " is not a valid readtext option")
# assign the key-value
opts <- list(value)
names(opts) <- paste0("readtext_", key)
options(opts)
}
# returns default options
get_options_default <- function(){
opts <- list(verbosity = 1)
return(opts)
}