-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers-generic.R
72 lines (65 loc) · 1.63 KB
/
helpers-generic.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
# Inspired by Thomas Mock's tidytuesdaymeta package.
next_tuesday <- function() {
todays_date <- lubridate::today(tzone = "America/New_York")
# Tuesday is 3. How many days are we before Tuesday?
diff_tuesday <- 3 - lubridate::wday(
todays_date,
week_start = 7 # Code explicitly to avoid strangeness.
)
if (diff_tuesday < 0) {
diff_tuesday <- diff_tuesday + 7
}
return(todays_date + diff_tuesday)
}
next_week_num <- function() {
week_date <- next_tuesday()
year <- lubridate::year(week_date)
jan_1st <- paste0(year, "0101")
jan_1st <- lubridate::ymd(jan_1st)
week_num <- as.numeric((week_date - jan_1st))/7 + 1
return(floor(week_num))
}
next_year <- function() {
week_date <- next_tuesday()
year <- lubridate::year(week_date)
return(year)
}
read_post_vars <- function() {
post_vars <- read_yaml_or_null(next_file("meta.yaml")) %||%
read_yaml_or_null(next_file("meta.yml")) %||%
read_yaml_or_null(next_file("post_vars.yaml")) %||%
read_yaml_or_null(next_file("post_vars.yml")) %||%
read_yaml_or_null(next_file("post_vars.json"))
if (length(post_vars)) {
return(post_vars)
}
cli::cli_abort("Failed to load post_vars.")
}
read_yaml_or_null <- function(url) {
tryCatch(
yaml::read_yaml(url),
error = function(e) {
return(NULL)
},
warning = function(e) {
return(NULL)
}
)
}
next_dir <- function() {
return(
file.path(
"https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data",
next_year(),
next_tuesday()
)
)
}
next_file <- function(filename) {
return(
file.path(
next_dir(),
filename
)
)
}