-
Notifications
You must be signed in to change notification settings - Fork 1
/
options_spec.R
233 lines (202 loc) · 7.27 KB
/
options_spec.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#' Specify Option
#'
#' An option specification outlines the various behaviors of an option. It's
#' default value, related global R option, and related environment variable
#' name, as well as a description. This information defines the operating
#' behavior of the option.
#'
#' @param name A string representing the internal name for the option. This is
#' the short form `<option>` used within a namespace and relates to, for
#' example, `<package>.<option>` global R option.
#' @param default Either a quoted expression (if parameter `quote == TRUE`) or
#' default value for the option. Defaults to an empty expression, indicating
#' that it is unset. The default value is lazily evaluated, evaluated only
#' when the option is first requested unless parameter `eager == TRUE`.
#' @param desc A written description of the option's effects
#' @param option_name,envvar_name A character value or function. If a character
#' value is provided it is used as the corresponding global option name or
#' environment variable name. If a function is provided it is provided with
#' the package name and internal option name to derive the global option name.
#' For example, provided with package `"mypkg"` and option `"myoption"`, the
#' function might return global option name `"mypkg.myoption"` or environment
#' variable name `"R_MYPKG_MYOPTION"`. Defaults to configured default
#' functions which fall back to `option_name_default` and
#' `envvar_name_default`, and can be configured using `set_option_name_fn`
#' and `set_envvar_name_fn`.
#' @param envvar_fn A function to use for parsing environment variable values.
#' Defaults to `envvar_eval_or_raw()`.
#' @param quoted A logical value indicating whether the `default` argument
#' should be treated as a quoted expression or as a value.
#' @param eager A logical value indicating whether the `default` argument should
#' be eagerly evaluated (upon call), or lazily evaluated (upon first use).
#' This distinction will only affect default values that rely on evaluation of
#' an expression, which may produce a different result depending on the
#' context in which it is evaluated.
#' @param envir An environment in which to search for an options envir object.
#' It is rarely necessary to use anything but the default.
#'
#' @return An `option_spec` object, which is a simple S3 class wrapping a list
#' containing these arguments.
#'
#' @importFrom utils packageName
#' @export
option_spec <- function(
name,
default = bquote(),
desc = NULL,
option_name = get_option_name_fn(envir),
envvar_name = get_envvar_name_fn(envir),
envvar_fn = envvar_eval_or_raw(),
quoted = FALSE,
eager = FALSE,
envir = parent.frame()
) {
package <- pkgname(envir)
if (!missing(default) && !quoted && !eager)
default <- match.call()[["default"]]
if (quoted && eager)
default <- eval(default, envir = envir)
if (is.function(option_name))
option_name <- option_name(package, name)
if (is.function(envvar_name))
envvar_name <- envvar_name(package, name)
structure(
list(
name = name,
expr = default,
desc = desc,
option_name = option_name,
envvar_name = envvar_name,
envvar_fn = envvar_fn,
envir = envir
),
class = "option_spec"
)
}
is_option_spec <- function(x) {
inherits(x, "option_spec")
}
are_option_spec <- function(x) {
vapply(x, is_option_spec, logical(1L))
}
#' @exportS3Method print option_spec
print.option_spec <- function(x, ...) {
cat(format(x, ...))
}
#' Format an option specification
#'
#' @param x An option specification ("option_spec") class object
#' @param value Optionally, the current value to display for the option being
#' specified
#' @param ... Additional arguments unused
#' @param fmt A list of formats to use for formatting individual text elements
#'
#' @return A formatted character value
#'
#' @keywords internal
#' @exportS3Method format option_spec
format.option_spec <- function(x, value, ..., fmt = options_fmts()) {
if (!is.null(x$desc)) {
desc <- paste(collapse = "\n\n", lapply(
strsplit(x$desc, "\n\n")[[1]],
function(line) {
paste(strwrap(line, exdent = 2, indent = 2), collapse = "\n")
}
))
} else {
desc <- NULL
}
envvar_help <- sprintf(
" (%s)",
attr(x$envvar_fn, "desc")
)
src <- opt_source(x)
paste0(
# name
fmt$name(x$name), " = ", format_value(value, fmt = fmt),
# description
"\n\n", sprintf("%s\n\n", fmt$desc(desc)),
# defaults
" ", format_field("option", src == "option", fmt$optname(x$option_name), fmt), "\n",
" ", format_field("envvar", src == "envir", fmt$optname(x$envvar_name), fmt), envvar_help, "\n",
" ", format_field("default", src == "default", deparse(x$expr), fmt),
collapse = ""
)
}
#' Format a possible option source
#'
#' @param field The field for the option source
#' @param active Whether this source is the source of the option's value
#' @param value The value from this source that was used
#' @inheritParams format.option_spec
#' @return A formatted character value
#'
#' @keywords internal
format_field <- function(field, active, value, fmt = options_fmts()) {
f <- if (active) fmt$field_active else fmt$field_inactive
paste0(
fmt$fade(if (active) "*" else " "),
f(field),
strrep(" ", 7 - nchar(field)),
fmt$fade(" : "),
value
)
}
#' Format value shorthands for command line display
#'
#' @param x An R object to display, attempting to show the actual value, but
#' falling back to shorthands for more complex data types.
#' @inheritParams format.option_spec
#' @return A formatted character value
#'
#' @keywords internal
format_value <- function(x, ..., fmt = NULL) {
if (missing(x)) return("")
UseMethod("format_value")
}
format_value.default <- function(x, ..., fmt = options_fmts()) {
if (isS4(x))
UseMethod("format_value", structure(list(), class = "S4"))
if (!is.null(attr(x, "class")))
UseMethod("format_value", structure(list(), class = "S3"))
str <- deparse(x)
fmt$shorthand(paste0(
substring(str[[1]], 1, 40),
if (length(str) > 1 || nchar(str[[1]]) > 40) " ..."
))
}
#' @name format_value
format_value.S3 <- function(x, ..., fmt = options_fmts()) {
fmt$shorthand(sprintf("<class: %s>", paste0(class(x), collapse = ", ")))
}
#' @name format_value
format_value.S4 <- function(x, ..., fmt = options_fmts()) {
fmt$shorthand(sprintf("<S4: %s>", paste0(class(x), collapse = ", ")))
}
#' @name format_value
format_value.function <- function(x, ..., fmt = options_fmts()) {
fmt$shorthand(paste0("fn(", paste0(names(formals(x)), collapse = ", "), ")"))
}
#' @importFrom utils capture.output
#' @name format_value
format_value.environment <- function(x, ..., fmt = options_fmts()) {
fmt$shorthand(utils::capture.output(print(x))[[1]])
}
#' @name format_value
format_value.expression <- function(x, ..., fmt = options_fmts()) {
fmt$shorthand("<expr>")
}
#' @name format_value
format_value.quote <- function(x, ..., fmt = options_fmts()) {
fmt$shorthand("<quote>")
}
#' @name format_value
format_value.call <- function(x, ..., fmt = options_fmts()) {
fmt$shorthand("<call>")
}
#' @name format_value
format_value.name <- function(x, ..., fmt = options_fmts()) {
fmt$shorthand(paste0("`", as.character(x), "`"))
}
#' @name format_value
format_value.symbol <- format_value.name