/
rholiday.R
232 lines (205 loc) · 6.07 KB
/
rholiday.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
#' Create a recurring holiday
#'
#' @description
#' `rholiday()` is used to create custom holidays. It wraps up a holiday `name`
#' and its corresponding `rschedule` into a holiday object with special
#' properties.
#'
#' Holiday objects can be tweaked with [hol_rename()], [hol_observe()], and
#' [hol_offset()], and they can be added to a calendar with [rcalendar()].
#'
#' @param rschedule `[rschedule]`
#'
#' The recurrence schedule that determines when the holiday occurs.
#'
#' @param name `[character(1)]`
#'
#' The name of the holiday. This serves as a unique identifier when adding
#' multiple holidays to an [rcalendar()].
#'
#' @export
#' @examples
#' on_christmas <- yearly() %>%
#' recur_on_month_of_year("Dec") %>%
#' recur_on_day_of_month(25)
#'
#' # Bundle a holiday name with its recurrence schedule to create a holiday
#' rholiday(on_christmas, "Christmas")
#'
#' # This is how the built in holiday objects are created
#' hol_christmas()
rholiday <- function(rschedule, name) {
check_rschedule(rschedule)
check_name(name)
new_rholiday(
name = name,
robserved = rschedule,
runobserved = rschedule
)
}
new_rholiday <- function(name,
robserved,
runobserved,
...,
class = character()) {
new_rschedule(
name = name,
robserved = robserved,
runobserved = runobserved,
...,
class = c(class, "almanac_rholiday")
)
}
# ------------------------------------------------------------------------------
#' @export
print.almanac_rholiday <- function(x, ...) {
cli::cli_text("<{x$name}>")
cli_indented()
print(rholiday_robserved(x))
cli::cli_end()
invisible(x)
}
# ------------------------------------------------------------------------------
#' @export
rschedule_events.almanac_rholiday <- function(x) {
x <- rholiday_robserved(x)
rschedule_events(x)
}
# ------------------------------------------------------------------------------
#' Holiday utility functions
#'
#' @description
#' These three functions allow you to tweak existing holidays created by
#' [rholiday()] so that they more properly align with business calendars. The
#' resulting holidays can then be added into an [rcalendar()].
#'
#' - `hol_observe()` adjusts a holiday based on when it is actually observed.
#' For example, many holidays that occur on a Saturday are actually observed
#' on the preceding Friday or following Monday.
#'
#' - `hol_offset()` creates a new holiday by _offsetting_ it from an existing
#' one. For example, Boxing Day is the day after Christmas, and the observance
#' of Boxing Day may be dependent on the observance of Christmas (i.e. if
#' Christmas is Sunday, it may be observed on Monday, so Boxing Day would be
#' observed on Tuesday).
#'
#' - `hol_rename()` renames an existing holiday. This is typically useful after
#' a call to `hol_offset()`, since it doesn't rename the holiday but you may
#' want to give it a different name.
#'
#' @inheritParams radjusted
#' @inheritParams roffset
#'
#' @param x `[rholiday]`
#'
#' An rholiday.
#'
#' @param name `[character(1)]`
#'
#' A new name for the holiday.
#'
#' @name holiday-utilities
#' @examples
#'
#' on_weekends <- weekly() %>%
#' recur_on_weekends()
#'
#' # Christmas, adjusted to nearest Friday or Monday if it falls on a weekend
#' on_christmas <- hol_christmas() %>%
#' hol_observe(on_weekends, adj_nearest)
#'
#' # Boxing Day is the day after Christmas.
#' # If observed Christmas is a Friday, then observed Boxing Day should be Monday.
#' # If observed Christmas is a Monday, then observed Boxing Day should be Tuesday.
#' on_boxing_day <- on_christmas %>%
#' hol_offset(1) %>%
#' hol_observe(on_weekends, adj_following) %>%
#' hol_rename("Boxing Day")
#'
#' christmas_dates <- alma_events(on_christmas, year = 2010:2015)
#' boxing_day_dates <- alma_events(on_boxing_day, year = 2010:2015)
#'
#' data.frame(
#' christmas = christmas_dates,
#' boxing_day = boxing_day_dates,
#' christmas_weekday = lubridate::wday(christmas_dates, label = TRUE),
#' boxing_day_weekday = lubridate::wday(boxing_day_dates, label = TRUE)
#' )
NULL
#' @export
#' @rdname holiday-utilities
hol_observe <- function(x, adjust_on, adjustment) {
check_rholiday(x)
name <- rholiday_name(x)
robserved <- rholiday_robserved(x)
runobserved <- rholiday_runobserved(x)
robserved <- radjusted(robserved, adjust_on, adjustment)
new_rholiday(
name = name,
robserved = robserved,
runobserved = runobserved
)
}
#' @export
#' @rdname holiday-utilities
hol_offset <- function(x, by) {
check_rholiday(x)
name <- rholiday_name(x)
robserved <- rholiday_robserved(x)
runobserved <- rholiday_runobserved(x)
robserved <- roffset(robserved, by)
runobserved <- roffset(runobserved, by)
new_rholiday(
name = name,
robserved = robserved,
runobserved = runobserved
)
}
#' @export
#' @rdname holiday-utilities
hol_rename <- function(x, name) {
check_rholiday(x)
check_name(name)
new_rholiday(
name = name,
robserved = rholiday_robserved(x),
runobserved = rholiday_runobserved(x)
)
}
# ------------------------------------------------------------------------------
check_rholiday <- function(x,
...,
allow_null = FALSE,
arg = caller_arg(x),
call = caller_env()) {
check_inherits(
x = x,
what = "almanac_rholiday",
allow_null = allow_null,
arg = arg,
call = call
)
}
list_check_all_rholidays <- function(x,
...,
arg = caller_arg(x),
call = caller_env()) {
for (i in seq_along(x)) {
check_rholiday(
x = x[[i]],
arg = cli::format_inline("{arg}[[{i}]]"),
call = call
)
}
invisible(NULL)
}
# ------------------------------------------------------------------------------
rholiday_robserved <- function(x) {
x$robserved
}
rholiday_runobserved <- function(x) {
x$runobserved
}
rholiday_name <- function(x) {
x$name
}