-
Notifications
You must be signed in to change notification settings - Fork 4
/
stepper.R
306 lines (270 loc) · 8.2 KB
/
stepper.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
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
#' Create a new stepper
#'
#' @description
#' - `stepper()` returns a function that can be used to add or subtract a
#' number of days from a Date, "stepping" over events specified by an
#' rschedule. You supply it the rschedule to step relative to, and then
#' call the returned function with the number of days to step by.
#'
#' - `workdays()` is a convenient stepper for stepping over the weekend.
#'
#' - `%s+%` steps forwards.
#'
#' - `%s-%` steps backwards.
#'
#' You _must_ use `%s+` and `%s-%` to control the stepping. `+` and `-` will
#' not work due to limitations in R's S3 dispatch system. Alternatively, you
#' can call [vctrs::vec_arith()] directly, which powers `%s+%` with a correct
#' double dispatch implementation.
#'
#' @details
#' Internally, a stepper is just powered by [alma_step()], so feel free to
#' use that directly.
#'
#' @inheritParams alma_step
#' @inheritParams daily
#'
#' @param x,y `[objects]`
#'
#' Objects to perform step arithmetic on. Typically Dates or steppers.
#'
#' @return
#' - `stepper()` returns a function of 1 argument, `n`, that can be used to
#' step by `n` days, relative to the rschedule.
#'
#' - `workdays()` return a new stepper object.
#'
#' - `%s+%` and `%s-%` return a new shifted Date vector.
#'
#' @export
#' @examples
#' # A Thursday and Friday
#' x <- as.Date(c("1970-01-01", "1970-01-02"))
#'
#' # Thursday is stepped forward 1 working day to Friday,
#' # and then 1 more working day to Monday.
#' # Friday is stepped forward 1 working day to Monday,
#' # and then 1 more working day to Tuesday
#' x %s+% workdays(2)
#'
#' # ---------------------------------------------------------------------------
#'
#' on_weekends <- weekly() %>%
#' recur_on_weekends()
#'
#' on_christmas <- yearly() %>%
#' recur_on_day_of_month(25) %>%
#' recur_on_month_of_year("Dec")
#'
#' rb <- runion(on_weekends, on_christmas)
#'
#' workday <- stepper(rb)
#'
#' # Friday before Christmas, which was on a Monday
#' friday_before_christmas <- as.Date("2000-12-22")
#'
#' # Steps over the weekend and Christmas to the following Tuesday
#' friday_before_christmas %s+% workday(1)
#'
#' # ---------------------------------------------------------------------------
#'
#' # Christmas in 2005 was on a Sunday, but your company probably "observed"
#' # it on Monday. So when you are on the Friday before Christmas in 2005,
#' # stepping forward 1 working day should go to Tuesday.
#'
#' # We'll adjust the previous rule for Christmas to roll to the nearest
#' # non-weekend day, if it happened to fall on a weekend.
#' on_observed_christmas <- radjusted(
#' on_christmas,
#' adjust_on = on_weekends,
#' adjustment = adj_nearest
#' )
#'
#' # Note that the "observed" date for Christmas is the 26th
#' alma_search("2005-01-01", "2006-01-01", on_observed_christmas)
#'
#' rb2 <- runion(on_weekends, on_observed_christmas)
#'
#' workday2 <- stepper(rb2)
#'
#' friday_before_christmas_2005 <- as.Date("2005-12-23")
#'
#' # Steps over the weekend and the observed Christmas date
#' # of 2005-12-26 to Tuesday the 27th.
#' friday_before_christmas_2005 %s+% workday2(1)
stepper <- function(rschedule) {
function(n) {
n <- vec_cast(n, integer(), x_arg = "n")
new_stepper(n = n, rschedule = rschedule)
}
}
# ------------------------------------------------------------------------------
#' @rdname stepper
#' @export
`%s+%` <- function(x, y) {
vec_arith("+", x, y)
}
#' @rdname stepper
#' @export
`%s-%` <- function(x, y) {
vec_arith("-", x, y)
}
# ------------------------------------------------------------------------------
#' @rdname stepper
#' @export
workdays <- function(n, since = NULL, until = NULL) {
rschedule <- weekly(since = since, until = until)
rschedule <- recur_on_weekends(rschedule)
workdays_stepper <- stepper(rschedule)
workdays_stepper(n)
}
# ------------------------------------------------------------------------------
new_stepper <- function(n = integer(), rschedule = daily()) {
if (!is_integer(n)) {
abort("`n` must be an integer.")
}
check_rschedule(rschedule)
new_vctr(
.data = n,
rschedule = rschedule,
class = "almanac_stepper",
inherit_base_type = FALSE
)
}
# ------------------------------------------------------------------------------
#' @export
vec_ptype_abbr.almanac_stepper <- function(x, ...) {
"stepper"
}
#' @export
vec_ptype_full.almanac_stepper <- function(x, ...) {
"stepper"
}
# ------------------------------------------------------------------------------
#' vctrs compatibility functions
#'
#' These functions are the extensions that allow stepper objects to
#' work with vctrs.
#'
#' @param x,y,to Objects.
#' @param op An arithmetic operator as a string.
#' @param x_arg,y_arg,to_arg Used to pass along error message information.
#' @param ... Used to pass along error message information.
#'
#' @return
#' See the corresponding vctrs function for the exact return value.
#'
#' @name almanac-vctrs-compat
#' @keywords internal
#'
#' @examples
#' library(vctrs)
#' vec_arith("+", as.Date("2019-01-04"), workdays(1))
NULL
# ------------------------------------------------------------------------------
#' @rdname almanac-vctrs-compat
#' @export vec_arith.almanac_stepper
#' @method vec_arith almanac_stepper
#' @export
vec_arith.almanac_stepper <- function(op, x, y, ...) {
UseMethod("vec_arith.almanac_stepper", y)
}
#' @method vec_arith.almanac_stepper default
#' @export
vec_arith.almanac_stepper.default <- function(op, x, y, ...) {
stop_incompatible_op(op, x, y)
}
#' @method vec_arith.almanac_stepper MISSING
#' @export
vec_arith.almanac_stepper.MISSING <- function(op, x, y, ...) {
switch(
op,
`+` = plus_stepper_missing(x),
`-` = minus_stepper_missing(x),
stop_incompatible_op(op, x, y)
)
}
plus_stepper_missing <- function(x) {
x
}
minus_stepper_missing <- function(x) {
rschedule <- stepper_rschedule(x)
x <- unclass(x)
x <- -x
new_stepper(x, rschedule)
}
# Note that `stepper(1, daily()) - Sys.Date()` is not defined!
#' @method vec_arith.almanac_stepper Date
#' @export
vec_arith.almanac_stepper.Date <- function(op, x, y, ...) {
switch(
op,
`+` = plus_stepper_date(x, y),
stop_incompatible_op(op, x, y)
)
}
plus_stepper_date <- function(x, y) {
rschedule <- stepper_rschedule(x)
n <- unclass(x)
alma_step(y, n, rschedule)
}
#' @method vec_arith.Date almanac_stepper
#' @export
vec_arith.Date.almanac_stepper <- function(op, x, y, ...) {
switch(
op,
`+` = plus_date_stepper(x, y),
`-` = minus_date_stepper(x, y),
stop_incompatible_op(op, x, y)
)
}
plus_date_stepper <- function(x, y) {
rschedule <- stepper_rschedule(y)
n <- unclass(y)
alma_step(x, n, rschedule)
}
minus_date_stepper <- function(x, y) {
rschedule <- stepper_rschedule(y)
n <- unclass(y)
n <- -n
alma_step(x, n, rschedule)
}
# ------------------------------------------------------------------------------
# vec_ptype2()
#' @rdname almanac-vctrs-compat
#' @export
vec_ptype2.almanac_stepper.almanac_stepper <- function(x, y, ..., x_arg = "", y_arg = "") {
if (!stepper_identical_rschedules(x, y)) {
details <- "Steppers must have identical rschedules to be coercible."
stop_incompatible_type(x, y, x_arg = x_arg, y_arg = y_arg, details = details)
}
new_stepper(rschedule = stepper_rschedule(x))
}
# ------------------------------------------------------------------------------
# vec_cast()
#' @rdname almanac-vctrs-compat
#' @export
vec_cast.almanac_stepper.almanac_stepper <- function(x, to, ..., x_arg = "", to_arg = "") {
if (!stepper_identical_rschedules(x, to)) {
details <- "Steppers must have identical rschedules to be coercible."
stop_incompatible_cast(x, to, x_arg = x_arg, to_arg = to_arg, details = details)
}
x
}
# ------------------------------------------------------------------------------
# slider_plus() / slider_minus()
# @export - .onLoad()
slider_plus.Date.almanac_stepper <- function(x, y) {
vec_arith("+", x, y)
}
# @export - .onLoad()
slider_minus.Date.almanac_stepper <- function(x, y) {
vec_arith("-", x, y)
}
# ------------------------------------------------------------------------------
stepper_rschedule <- function(x) {
attr(x, "rschedule", exact = TRUE)
}
stepper_identical_rschedules <- function(x, y) {
identical(stepper_rschedule(x), stepper_rschedule(y))
}