-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathutil.R
145 lines (138 loc) · 3.29 KB
/
util.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
#' Returns rb3 package cache directory
#'
#' Returns rb3 package cache directory
#'
#' @details
#' In order to set a default directory for cache, which is a good idea for those
#' who want to increase data historically, the option `rb3.cachedir` can be
#' set.
#' Once it is set, the defined directory will be used as the default cachedir.
#'
#' @return a string with the file path of rb3 cache directory
#'
#' @examples
#' cachedir()
#' @export
cachedir <- function() {
cache_folder <- getOption("rb3.cachedir")
cache_folder <- if (is.null(cache_folder)) {
file.path(tempdir(), "rb3-cache")
} else {
cache_folder
}
if (!dir.exists(cache_folder)) {
dir.create(cache_folder, recursive = TRUE)
}
cache_folder
}
#' Clear cache directory
#'
#' Clear cache directory
#'
#' @return Has no return
#'
#' @examples
#' \dontrun{
#' clearcache()
#' }
#' @export
clearcache <- function() {
cache_folder <- cachedir()
unlink(cache_folder, recursive = TRUE)
}
#' Fetches a single marketdata
#'
#' @param idx_date index of data (1.. n_dates)
#' @param date_vec Vector of dates
#' @param cache_folder Location of cache folder (default = cachedir())
#' @param do_cache Whether to use cache or not (default = TRUE)
#' @param ... orther arguments
#'
#' @return
#' A dataframe or `NULL`
#'
#' @noRd
get_single_marketdata <- function(template,
idx_date,
date_vec,
cache_folder,
do_cache, ...) {
refdate <- date_vec[idx_date]
fname <- download_marketdata(template, cache_folder, do_cache,
refdate = refdate, ...
)
if (!is.null(fname)) {
read_marketdata(fname, template, TRUE, do_cache)
} else {
alert("danger", "Error: no data found for date {refdate}",
refdate = refdate
)
return(NULL)
}
}
#' cli_progress_along wrapper
#'
#' @param x data to iterate through
#' @param func function to call
#' @param msg message to display
#' @param ... orther arguments
#'
#' @return
#' A list with `func` returned values
#'
#' @noRd
log_map_process_along <- function(x, func, msg, ...) {
f_ <- paste(
"{pb_spin}",
"{msg}",
"{pb_current}/{pb_total}",
"|",
"{pb_bar}",
"{pb_percent}",
"|",
"{pb_eta_str}"
)
rb3_hide_progressbar <- getOption("rb3.silent")
if (!is.null(rb3_hide_progressbar) && isTRUE(rb3_hide_progressbar)) {
map(seq_along(x), func, ...)
} else {
map(cli_progress_along(x, format = f_), func, ...)
}
}
#' cli_alert_* wrapper
#'
#' @param x type
#' @param text message to display
#' @param ... orther arguments
#'
#' @return
#' A list with `func` returned values
#'
#' @noRd
alert <- function(x = c("info", "success", "danger", "warning"), text, ...) {
x <- match.arg(x)
rb3_silent <- getOption("rb3.silent")
if (!is.null(rb3_silent) && isTRUE(rb3_silent)) {
# do nothing
} else {
f_ <- alert_fun(x)
if (!is.null(f_)) {
f_(str_glue(text, .envir = as.environment(list(...))))
}
}
}
alert_fun <- function(x) {
funcs <- list(
info = cli_alert_info,
danger = cli_alert_danger,
success = cli_alert_success,
warning = cli_alert_warning
)
func <- funcs[[x]]
if (is.null(func)) {
warning(paste0("Invalid call to alert function ", x))
return(NULL)
} else {
func
}
}