/
utilities.R
322 lines (250 loc) · 6.94 KB
/
utilities.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
307
308
309
310
311
312
313
314
315
316
317
318
319
#' @title Find an Object's Host Package and Version Information
#'
#' @description Locate basic package information of an R object. If nothing is input, it operates on itself. \cr
#' If the object is sourced through multiple packages, each package and its version are included.
#'
#' @param what An R object.
#'
#' @return `list` of the object, its class, the packages it is sourced from / through, and the versions of those packages.
#'
#' @name package_version_finder
#'
#' @family utilities
#'
#' @examples
#' package_version_finder( )
#'
NULL
#' @rdname package_version_finder
#'
#' @export
#'
package_version_finder <- function (what) {
if (missing(what)) {
what <- "package_version_finder"
}
object_expr <- parse(text = what)
object_eval <- eval(expr = object_expr)
object_class <- class(x = object_eval)
helps <- help.search(pattern = what,
agrep = FALSE)
packages_names <- helps$matches$Package
packages_versions <- sapply(X = packages_names,
FUN = packageDescription,
fields = "Version")
names(packages_versions) <- packages_names
list(object = what,
class = object_class,
package = packages_names,
version = packages_versions)
}
#' @title Round an Interpolated Series
#'
#' @description Wraps [`round`] around [`forecast::na.interp`] to provide a rounded interpolated series, which is then enforced to be greater than or equal to a minimum value (default `min_val = 0`) via [`pmax`].
#'
#' @param x A time series passed directly to [`forecast::na.interp`].
#'
#' @param lambda Box-Cox transformation parameter passed directly to [`forecast::na.interp`].
#'
#' @param linear `logical` indicator of if linear interpolation should be used. Passed directly to [`forecast::na.interp`].
#'
#' @param digits `integer` or `numeric` integer of how many digits to round to. Passed directly to [`round`].
#'
#' @param min_val `integer` or `numeric` integer of minimum value allowable in the series.
#'
#' @return `numeric` series.
#'
#' @family utilities
#'
#' @name round_na.interp
#'
#' @examples
#' round_na.interp(x = c(1, 2, 3, NA, NA, 170))
#' round_na.interp(x = c(-1, 2, 3, NA, NA, 170), min_val = 1)
#'
NULL
#' @rdname round_na.interp
#'
#' @export
#'
round_na.interp <- function (x,
lambda = NULL,
linear = (frequency(x) <= 1 | sum(!is.na(x)) <= 2 * frequency(x)),
digits = 0,
min_val = 0) {
xi <- na.interp(x = x,
lambda = lambda,
linear = linear)
xr <- round(x = xi,
digits = digits)
pmax(... = min_val,
... = xr)
}
#' @title Determine a File's Extension
#'
#' @description Based on the separating character, determine the file extension.
#'
#' @param path `character` value of the file path possibly with an extension.
#'
#' @param sep_char `character` value of the separator that delineates the extension from the file path. \cr
#' Generally, this will be `."`, but for some API URLs, the extension is actually a query component, so the separator may sometimes need to be `"="`.
#'
#' @return `character` value of the extension (`file_ext`).
#'
#' @name file_ext
#'
#' @family utilities
#'
#' @examples
#' file_ext("home/folders.with.dots/stuff/ok.csv")
#' file_ext(NMME_urls()[[1]])
#' file_ext(NMME_urls()[[1]], "=")
#'
NULL
#' @rdname file_ext
#'
#' @export
#'
file_ext <- function (path, sep_char = ".") {
for_regexpr <- paste0("\\", sep_char, "([[:alnum:]]+)$")
pos <- regexpr(for_regexpr, path)
ifelse(pos > -1L, substring(path, pos + 1L), "")
}
#' @title Create a Named Empty List
#'
#' @description Produces a list with `NULL` for each element named according to `element_names`.
#'
#' @param element_names `character` vector of names for the elements in the list.
#'
#' @return `list` with names `element_names` and values `NULL`.
#'
#' @name named_null_list
#'
#' @family utilities
#'
#' @examples
#' named_null_list(c("a", "b", "c"))
#'
NULL
#' @rdname named_null_list
#'
#' @export
#'
named_null_list <- function (element_names = NULL) {
return_if_null(element_names)
nelements <- length(element_names)
out <- vector("list", nelements)
names(out) <- element_names
out
}
#' @title Update a List's Elements
#'
#' @description Update a list with new values for elements
#'
#' @param list `list` to be updated with `...`.
#'
#' @param ... Named elements to update in `list`
#'
#' @return Updated `list`.
#'
#' @name update_list
#'
#' @family utilities
#'
#' @examples
#' orig_list <- list(a = 1, b = 3, c = 4)
#' update_list(orig_list)
#' update_list(orig_list, a = "a")
#' update_list(orig_list, a = 10, b = NULL)
#'
NULL
#' @rdname update_list
#'
#' @export
#'
update_list <- function (list = list(),
...) {
if (!is.list(list)) {
stop("`list` must be a list")
}
update_elems <- list(...)
nupdate_elems <- length(update_elems)
norig_elems <- length(list)
updated_list <- named_null_list(element_names = names(list))
if (norig_elems > 0) {
for (i in 1:norig_elems) {
if (!is.null(list[[i]])) {
updated_list[[i]] <- list[[i]]
}
}
}
if (nupdate_elems > 0) {
names_update_elems <- names(update_elems)
for (i in 1:nupdate_elems) {
if (!is.null(update_elems[[i]])) {
updated_list[[names_update_elems[i]]] <- update_elems[[i]]
}
}
}
updated_list
}
#' @title Calculate the Fraction of the Year from a Date
#'
#' @description Based on the year in which the date occurred, determine the fraction of the year (foy) for the date (in relation to New Year's Eve in that year).
#'
#' @param dates `Date`(s) or `Date`-conformable value(s) to be converted to the fraction of the year.
#'
#' @return `numeric` value(s) of the fraction of the year.
#'
#' @name foy
#'
#' @family utilities
#'
#' @examples
#' Sys.Date( )
#' foy(Sys.Date())
#'
NULL
#' @rdname foy
#'
#' @export
#'
foy <- function (dates = NULL) {
return_if_null(dates)
dates <- as.Date(dates)
jday <- as.numeric(format(dates, "%j"))
nye <- as.Date(paste0(format(dates, "%Y"), "-12-31"))
nyejday <- as.numeric(format(nye, "%j"))
round(jday / nyejday, 3)
}
#' @title Replace a Value with an Alternative if it is NULL
#'
#' @description Replaces the focal input with the alternative value if it is `NULL`.
#'
#' @param x Focal input.
#'
#' @param alt Alternative value.
#'
#' @return `x` if not `NULL`, `alt` otherwise.
#'
#' @name ifnull
#'
#' @family utilities
#'
#' @examples
#' ifnull(NULL, 123)
#' ifnull(TRUE, 123)
#' ifnull(FALSE, 123)
#'
NULL
#' @rdname ifnull
#'
#' @export
#'
ifnull <- function (x = NULL, alt = NULL) {
if (is.null(x)) {
alt
} else {
x
}
}