Skip to content

Commit c1e3ff9

Browse files
committed
refactor validate_date() to avoid duplication
1 parent f5ef88b commit c1e3ff9

File tree

3 files changed

+32
-36
lines changed

3 files changed

+32
-36
lines changed

R/layer_add_forecast_date.R

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ layer_add_forecast_date <-
8181
}
8282

8383
layer_add_forecast_date_new <- function(forecast_date, id) {
84-
8584
layer("add_forecast_date", forecast_date = forecast_date, id = id)
8685
}
8786

@@ -93,27 +92,25 @@ slather.layer_add_forecast_date <- function(object, components, workflow, new_da
9392
workflow$fit$meta$max_time_value,
9493
max(new_data$time_value)
9594
)
96-
object$forecast_date <- max_time_value
95+
forecast_date <- max_time_value
96+
} else {
97+
forecast_date <- object$forecast_date
9798
}
9899

99100
expected_time_type <- attr(
100101
workflows::extract_preprocessor(workflow)$template, "metadata"
101102
)$time_type
102103
if (expected_time_type == "week") expected_time_type <- "day"
103-
check <- validate_date(object$forecast_date, expected_time_type)
104-
105-
if (!check$ok) {
106-
cli::cli_abort(c(
107-
"The `forecast_date` was given as a {.val {check$x}} while the",
108-
`!` = "`time_type` of the training data was {.val {check$expected}}.",
109-
i = "See {.topic epiprocess::epi_df} for how these are determined."
110-
))
111-
}
112-
104+
validate_date(forecast_date, expected_time_type,
105+
call = expr(layer_add_forecast_date())
106+
)
107+
forecast_date <- coerce_time_type(forecast_date, expected_time_type)
108+
object$forecast_date <- forecast_date
113109
components$predictions <- dplyr::bind_cols(
114110
components$predictions,
115-
forecast_date = coerce_time_type(object$forecast_date, expected_time_type)
111+
forecast_date = forecast_date
116112
)
113+
117114
components
118115
}
119116

R/layer_add_target_date.R

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -91,29 +91,20 @@ slather.layer_add_target_date <- function(object, components, workflow, new_data
9191
if (expected_time_type == "week") expected_time_type <- "day"
9292

9393
if (!is.null(object$target_date)) {
94-
check <- validate_date(object$target_date, expected_time_type)
95-
if (!check$ok) {
96-
cli::cli_abort(c(
97-
"The `target_date` was given as a {.val {check$x}} while the",
98-
`!` = "`time_type` of the training data was {.val {check$expected}}.",
99-
i = "See {.topic epiprocess::epi_df} for descriptions of these are determined."
100-
))
101-
}
102-
target_date <- coerce_time_type(object$target_date, expected_time_type)
94+
target_date <- object$target_date
95+
validate_date(target_date, expected_time_type,
96+
call = expr(layer_add_target_date())
97+
)
98+
target_date <- coerce_time_type(target_date, expected_time_type)
10399
} else if (
104100
detect_layer(the_frosting, "layer_add_forecast_date") &&
105-
!is.null(possible_fd <- extract_argument(
101+
!is.null(forecast_date <- extract_argument(
106102
the_frosting, "layer_add_forecast_date", "forecast_date"
107103
))) {
108-
check <- validate_date(possible_fd, expected_time_type)
109-
if (!check$ok) {
110-
cli::cli_abort(c(
111-
"The `forecast_date` was given as a {.val {check$x}} while the",
112-
`!` = "`time_type` of the training data was {.val {check$expected}}.",
113-
i = "See {.topic epiprocess::epi_df} for how these are determined."
114-
))
115-
}
116-
forecast_date <- coerce_time_type(possible_fd, expected_time_type)
104+
validate_date(forecast_date, expected_time_type,
105+
call = expr(layer_add_forecast_date())
106+
)
107+
forecast_date <- coerce_time_type(forecast_date, expected_time_type)
117108
ahead <- extract_argument(the_recipe, "step_epi_ahead", "ahead")
118109
target_date <- forecast_date + ahead
119110
} else {
@@ -126,6 +117,7 @@ slather.layer_add_target_date <- function(object, components, workflow, new_data
126117
target_date <- max_time_value + ahead
127118
}
128119

120+
object$target_date <- target_date
129121
components$predictions <- dplyr::bind_cols(components$predictions,
130122
target_date = target_date
131123
)

R/time_types.R

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,15 @@ coerce_time_type <- function(x, target_type) {
5959
)
6060
}
6161

62-
validate_date <- function(x, expected) {
63-
x <- guess_time_type(x)
64-
ok <- x == expected
65-
enlist(ok, x, expected)
62+
validate_date <- function(x, expected, arg = rlang::caller_arg(x),
63+
call = rlang::caller_env()) {
64+
time_type_x <- guess_time_type(x)
65+
ok <- time_type_x == expected
66+
if (!ok) {
67+
cli::cli_abort(c(
68+
"The {.arg {arg}} was given as a {.val {time_type_x}} while the",
69+
`!` = "`time_type` of the training data was {.val {expected}}.",
70+
i = "See {.topic epiprocess::epi_df} for descriptions of these are determined."
71+
), call = call)
72+
}
6673
}

0 commit comments

Comments
 (0)