Skip to content

Commit c1ca6d3

Browse files
committed
print fix and tests
1 parent f36f6fa commit c1ca6d3

File tree

5 files changed

+89
-42
lines changed

5 files changed

+89
-42
lines changed

R/step_adjust_latency.R

+45-8
Original file line numberDiff line numberDiff line change
@@ -280,11 +280,6 @@ bake.step_adjust_latency <- function(object, new_data, ...) {
280280
#' @export
281281
print.step_adjust_latency <-
282282
function(x, width = max(20, options$width - 35), ...) {
283-
if (length(x$terms) == 0) {
284-
terms <- "all previous predictors"
285-
} else {
286-
terms <- x$terms
287-
}
288283
if (!is.null(x$forecast_date)) {
289284
conj <- "with forecast date"
290285
extra_text <- x$forecast_date
@@ -299,9 +294,51 @@ print.step_adjust_latency <-
299294
conj <- "with latency"
300295
extra_text <- "set at train time"
301296
}
302-
print_epi_step(terms, terms, x$trained, x$method,
303-
conjunction = conj,
304-
extra_text = extra_text
297+
# what follows is a somewhat modified version of print_epi_step, since the case of no arguments for adjust_latency means apply to all relevant columns, and not none of them
298+
theme_div_id <- cli::cli_div(
299+
theme = list(.pkg = list(`vec-trunc` = Inf, `vec-last` = ", "))
300+
)
301+
# this is a slightly modified copy of
302+
title <- trimws(x$method)
303+
trained_text <- dplyr::if_else(x$trained, "Trained", "")
304+
vline_seperator <- dplyr::if_else(trained_text == "", "", "|")
305+
comma_seperator <- dplyr::if_else(
306+
trained_text != "", true = ",", false = ""
305307
)
308+
extra_text <- recipes::format_ch_vec(extra_text)
309+
width_title <- nchar(paste0(
310+
"* ", title, ":", " ", conj, " ", extra_text, " ", vline_seperator,
311+
" ", trained_text, " "
312+
))
313+
width_diff <- cli::console_width() * 1 - width_title
314+
if (x$trained) {
315+
elements <- x$columns
316+
} else {
317+
if (length(x$terms) == 0) {
318+
elements <- "all previous predictors"
319+
} else {
320+
elements <- lapply(x$terms, function(x) {
321+
rlang::expr_deparse(rlang::quo_get_expr(x), width = Inf)
322+
})
323+
elements <- vctrs::list_unchop(elements, ptype = character())
324+
}
325+
}
326+
327+
element_print_lengths <- cumsum(nchar(elements)) +
328+
c(0L, cumsum(rep(2L, length(elements) - 1))) +
329+
c(rep(5L, length(elements) - 1), 0L)
330+
first_line <- which(width_diff >= element_print_lengths)
331+
first_line <- unname(first_line)
332+
first_line <- ifelse(
333+
test = identical(first_line, integer(0)),
334+
yes = length(element_print_lengths),
335+
no = max(first_line)
336+
)
337+
more_dots <- ifelse(first_line == length(elements), "", ", ...")
338+
cli::cli_bullets(
339+
c("\n {title}: \\\n {.pkg {cli::cli_vec(elements[seq_len(first_line)])}}\\\n {more_dots} \\\n {conj} \\\n {.pkg {extra_text}} \\\n {vline_seperator} \\\n {.emph {trained_text}}")
340+
)
341+
342+
cli::cli_end(theme_div_id)
306343
invisible(x)
307344
}

man/epi_shift_single.Rd

-24
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# printing step_adjust_latency results in expected output
2+
3+
Code
4+
r5
5+
Message
6+
7+
-- Epi Recipe ------------------------------------------------------------------
8+
9+
-- Inputs
10+
Number of variables by role
11+
raw: 2
12+
geo_value: 1
13+
time_value: 1
14+
15+
-- Operations
16+
1. Lagging: death_rate by 0, 6, 11
17+
2. Lagging: case_rate by 1, 5
18+
3. Leading: death_rate by 7
19+
4. extend_lags: case_rate with latency set at train time
20+
21+
---
22+
23+
Code
24+
r
25+
Message
26+
27+
-- Epi Recipe ------------------------------------------------------------------
28+
29+
-- Inputs
30+
Number of variables by role
31+
raw: 2
32+
geo_value: 1
33+
time_value: 1
34+
35+
-- Operations
36+
1. Leading: death_rate by 7
37+
2. extend_ahead: all previous predictors with latency set at train time
38+
3. Lagging: death_rate by 0, 7, 14
39+

tests/testthat/test-epi_shift.R

-10
This file was deleted.

tests/testthat/test-step_adjust_latency.R

+5
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,9 @@ test_that("printing step_adjust_latency results in expected output", {
245245
step_epi_ahead(death_rate, ahead = ahead) %>%
246246
step_adjust_latency(case_rate, method = "extend_lags")
247247
expect_snapshot(r5)
248+
r <- epi_recipe(case_death_rate_subset) %>%
249+
step_epi_ahead(death_rate, ahead = 7) %>%
250+
step_adjust_latency(method = "extend_ahead") %>%
251+
step_epi_lag(death_rate, lag = c(0, 7, 14))
252+
expect_snapshot(r)
248253
})

0 commit comments

Comments
 (0)