-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths3-yr.R
More file actions
375 lines (328 loc) · 9.18 KB
/
s3-yr.R
File metadata and controls
375 lines (328 loc) · 9.18 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# yr.R
# S3 vector class `era_yr` (`yr`): years with an era
# Register formal class for S4 compatibility
# https://vctrs.r-lib.org/articles/s3-vector.html#implementing-a-vctrs-s3-class-in-a-package-1
#' @importFrom methods setOldClass
methods::setOldClass(c("era_yr", "vctrs_vctr"))
# Constructors ------------------------------------------------------------
#' Create a vector of years with era
#'
#' A `yr` object represents years with an associated calendar era or time scale.
#'
#' @param x A numeric vector of years.
#' @param era The calendar era used by `x`. Either:
#' * A string matching one of the standard era labels defined in [eras()]
#' * An `era` object constructed with [era()]
#'
#' @return
#' A `yr` (`era_yr`) object.
#'
#' @family years with era functions
#'
#' @export
#'
#' @examples
#' # The R Age
#' yr(1993:2020, "CE")
#'
#' # A bad movie
#' yr(10000, "BC")
yr <- function(x = numeric(), era = character()) {
x <- vec_cast(x, numeric())
era <- era(era)
yr <- new_yr(x, era)
validate_yr(yr)
return(yr)
}
new_yr <- function(x = numeric(), era = new_era()) {
new_vctr(x, era = era, class = "era_yr")
}
# Validators --------------------------------------------------------------
#' Validation functions for `yr` objects
#'
#' @description
#' Tests whether an object is a vector of years with an era (a `yr` object).
#' `is_yr()` tests whether the object inherits from the S3 class `era_yr`.
#' `is_valid_yr()` performs additional checks to determine whether the object
#' is well-formed (see details).
#' `validate_yr()` throws an informative error message for invalid `yr`s.
#'
#' @param x Object to test.
#'
#' @details
#' Valid `yr` objects:
#'
#' * Must contain numeric data (NAs are allowed)
#' * Must have the `era` attribute set and not NA
#' * Must not have more than one era
#' * Must have an `era` attribute that is a valid era object (see `validate_era()`)
#'
#' @return
#' `is_yr()` and `is_valid_yr()` return `TRUE` or `FALSE`.
#' `validate_yr()` returns `x` invisibly, and is used for its side-effect of
#' throwing an informative error for invalid objects.
#'
#' @family era helper functions
#'
#' @export
#'
#' @examples
#' x <- yr(5000:5050, era("cal BP"))
#' is_yr(x)
#' is_valid_yr(x)
#' validate_yr(x)
is_yr <- function(x) {
inherits(x, "era_yr")
}
#' @rdname is_yr
#' @export
validate_yr <- function(x) {
if (!is_yr(x)) {
abort("Invalid year vector:",
class = "era_invalid_yr",
body = format_error_bullets(c(
x = "Must inherit from class era_yr.",
i = "See ?yr for methods for constructing year vectors."
)))
}
problems <- yr_problems(x)
if (any(problems)) {
problems <- names(problems[problems])
names(problems) <- rep("x", length(problems))
abort("Invalid year vector:",
class = "era_invalid_yr",
body = format_error_bullets(problems))
}
return(invisible(x))
}
#' @rdname is_yr
#' @export
is_valid_yr <- function(x) {
if (!is_yr(x)) {
return(FALSE)
}
problems <- yr_problems(x)
!any(problems)
}
#' List problems with a yr object
#'
#' Internal function for testing validity of yr objects.
#'
#' @param x Object to test
#'
#' @return
#' A named logical vector. The names describe invalid conditions (formatted as
#' error messages that can be passed to vctrs::abort()) and `TRUE` indicates
#' an invalid state.
#'
#' @noRd
#' @keywords internal
yr_problems <- function(x) {
!c(
"yr data must be numeric" =
vec_is(vec_data(x), integer()) || vec_is(vec_data(x), numeric()),
"`era` attribute must be set and not NA" =
!any(is.null(yr_era(x))) && !any(is.na(yr_era(x))),
"Must not have more than one era" =
vec_size(yr_era(x)) <= 1,
"`era` attribute must be a valid era" =
ifelse(!any(is.null(yr_era(x))) && !any(is.na(yr_era(x))),
is_valid_era(yr_era(x)), TRUE)
)
}
# Casting/coercion --------------------------------------------------------
#' @export
vec_ptype2.era_yr.era_yr <- function(x, y, ..., x_arg = "", y_arg = "") {
if (yr_era(x) != yr_era(y)) {
stop_incompatible_type(x, y,
x_arg = x_arg, y_arg = y_arg,
action = "combine",
details = "Reconcile eras with yr_transform() first.")
}
new_yr(era = yr_era(x))
}
#' @export
vec_cast.era_yr.era_yr <- function(x, to, ..., x_arg = "", to_arg = "") {
if (yr_era(x) != yr_era(to)) {
stop_incompatible_cast(x, to,
x_arg = x_arg, to_arg = to_arg,
details = "Reconcile eras with yr_transform() first.")
}
if (era_label(yr_era(x)) != era_label(yr_era(to)) |
era_name(yr_era(x)) != era_name(yr_era(to))) {
warn(c("eras have different label or name parameters."),
class = "era_lossy_coercion")
}
new_yr(vec_data(x), era = yr_era(to))
}
#' @export
vec_ptype2.integer.era_yr <- function(x, y, ...) new_yr(era = yr_era(y))
#' @export
vec_ptype2.era_yr.integer <- function(x, y, ...) new_yr(era = yr_era(x))
#' @export
vec_cast.integer.era_yr <- function(x, to, ...) {
vec_cast(vec_data(x), integer())
}
#' @export
vec_cast.era_yr.integer <- function(x, to, ...) {
new_yr(vec_cast(x, numeric()), yr_era(to))
}
#' @export
vec_ptype2.double.era_yr <- function(x, y, ...) new_yr(era = yr_era(y))
#' @export
vec_ptype2.era_yr.double <- function(x, y, ...) new_yr(era = yr_era(x))
#' @export
vec_cast.double.era_yr <- function(x, to, ...) {
vec_cast(vec_data(x), double())
}
#' @export
vec_cast.era_yr.double <- function(x, to, ...) {
new_yr(x, yr_era(to))
}
#' @export
vec_cast.character.era_yr <- function(x, to, ...) {
paste(vec_data(x), era_label(yr_era(x)))
}
#' @export
vec_cast.era_yr.character <- function(x, to, ..., x_arg = "", to_arg = "") {
stop_incompatible_cast(x, to, x_arg = x_arg, to_arg = to_arg)
}
# Format/print ---------------------------------------------------------
#' @export
vec_ptype_full.era_yr <- function(x, ...) {
paste0("yr (", era_label(yr_era(x)), ")")
}
#' @export
vec_ptype_abbr.era_yr <- function(x, ...) {
"yr"
}
#' @export
obj_print_header.era_yr <- function(x, ...) {
xera <- era_label(yr_era(x))
cat("# ", xera, " years <", vec_ptype_abbr(x), "[", vec_size(x), "]>:", "\n",
sep = "")
}
#' @export
obj_print_footer.era_yr <- function(x, ...) {
cat("# Era: ", format(yr_era(x)), "\n", sep = "")
}
#' @importFrom pillar pillar_shaft
#' @export
pillar_shaft.era_yr <- function(x, ...) {
out <- format(
paste(vec_data(x), pillar::style_subtle(era_label(yr_era(x)))),
justify = "right"
)
pillar::new_pillar_shaft_simple(out, align = "right")
}
# Maths ----------------------------------------------------------
#' @method vec_arith era_yr
#' @export
vec_arith.era_yr <- function(op, x, y, ...) {
UseMethod("vec_arith.era_yr", y)
}
#' @method vec_arith.era_yr default
#' @export
vec_arith.era_yr.default <- function(op, x, y, ...) {
stop_incompatible_op(op, x, y)
}
#' @method vec_arith.era_yr era_yr
#' @export
vec_arith.era_yr.era_yr <- function(op, x, y, ...) {
y <- vec_cast(y, x)
switch(
op,
"+" = ,
"-" = new_yr(vec_arith_base(op, x, y), yr_era(x)),
"/" = ,
"*" = ,
"^" = ,
"%%" = ,
"%/%" = vec_arith_base(op, x, y),
stop_incompatible_op(op, x, y)
)
}
#' @method vec_arith.era_yr MISSING
#' @export
vec_arith.era_yr.MISSING <- function(op, x, y, ...) {
switch(op,
`-` = new_yr(x * -1, yr_era(x)),
`+` = x,
stop_incompatible_op(op, x, y)
)
}
#' @method vec_arith.era_yr numeric
#' @export
vec_arith.era_yr.numeric <- function(op, x, y, ...) {
switch(
op,
"+" = ,
"-" = new_yr(vec_arith_base(op, x, y), yr_era(x)),
"/" = ,
"*" = ,
"^" = ,
"%%" = ,
"%/%" = vec_arith_base(op, x, y),
stop_incompatible_op(op, x, y)
)
}
#' @method vec_arith.numeric era_yr
#' @export
vec_arith.numeric.era_yr <- function(op, x, y, ...) {
switch(
op,
"+" = ,
"-" = new_yr(vec_arith_base(op, x, y), yr_era(y)),
"/" = ,
"*" = ,
"^" = ,
"%%" = ,
"%/%" = vec_arith_base(op, x, y),
stop_incompatible_op(op, x, y)
)
}
# Get/set attributes -----------------------------------------------------
#' Get or set the era of a vector of years
#'
#' Functions for extracting or assigning the era of a vector of years.
#' This function does not alter the underlying values of `x`. Use [yr_transform()]
#' to *convert* the values of a `yr` vector to a new era.
#'
#' @param x A vector of years.
#' @param value,era An `era` object (see [era()]) to be assigned to `x`.
#'
#' @return
#' `yr_era(x)` returns the existing era associated with `x`.
#'
#' `yr_set_era(x, era)` and `yr_era(x) <- era` return `x` with the new era
#' assigned. If `x` is not already a `yr` vector, it will attempt to coerce it
#' into one.
#'
#' @family years with era functions
#'
#' @export
#'
#' @examples
#' x <- 5000:5050
#' yr_era(x) <- era("cal BP")
#' yr_era(x)
yr_era <- function(x) {
era <- attr(x, "era")
return(era)
}
#' @rdname yr_era
#' @export
yr_set_era <- function(x, era) {
era <- era(era)
if (!is_yr(x)) {
x <- new_yr(vec_cast(x, numeric()), era)
}
else {
attr(x, "era") <- era
}
validate_yr(x)
return(x)
}
#' @rdname yr_era
#' @export
`yr_era<-` <- function(x, value) yr_set_era(x, value)