-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.R
72 lines (56 loc) · 1.58 KB
/
utils.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
# Here below put your small tiny supporting functions -------------
`%||%` <- function(x, y) if (is.null(x)) y else x
view_in_excel <- function(.data) {
if (interactive()) {
tmp <- fs::file_temp("excel", ext = "csv")
readr::write_excel_csv(.data, tmp)
fs::file_show(tmp)
}
invisible(.data)
}
extract_fct_names <- function(path) {
readr::read_lines(path) |>
stringr::str_extract_all("^.*(?=`? ?<- ?function)") |>
unlist() |>
purrr::compact() |>
stringr::str_remove_all("[\\s`]+")
}
get_input_data_path <- function(x = "") {
file.path(
Sys.getenv("PRJ_SHARED_PATH"),
Sys.getenv("INPUT_DATA_FOLDER"),
x
) |>
normalizePath()
}
get_output_data_path <- function(x = "") {
file.path(
Sys.getenv("PRJ_SHARED_PATH"),
Sys.getenv("OUTPUT_DATA_FOLDER"),
x
) |>
normalizePath(mustWork = FALSE)
}
share_objects <- function(obj_list) {
now <- lubridate::now() |>
stringr::str_remove_all("\\W+") |>
stringr::str_sub(1, 12)
file_name_now <- stringr::str_c(
names(obj_list), "-", now, ".rds"
)
file_name_latest <- stringr::str_c(
names(obj_list), "-", "latest", ".rds"
)
obj_paths_now <- get_output_data_path(file_name_now) |>
normalizePath(mustWork = FALSE) |>
purrr::set_names(names(obj_list))
obj_paths_latest <- get_output_data_path(file_name_latest) |>
normalizePath(mustWork = FALSE) |>
purrr::set_names(names(obj_list))
# Those must be RDS
obj_list |>
purrr::walk2(obj_paths_now, readr::write_rds)
obj_list |>
purrr::walk2(obj_paths_latest, readr::write_rds)
obj_paths_latest
}