Skip to content

Commit

Permalink
feat(epi_slide_opt): adjust [.]new_col_name = error guidance
Browse files Browse the repository at this point in the history
  • Loading branch information
brookslogan committed Nov 12, 2024
1 parent b4d71dc commit 6db1391
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 3 deletions.
2 changes: 1 addition & 1 deletion R/slide.R
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ epi_slide_opt <- function(
if ("new_col_name" %in% provided_args || ".new_col_name" %in% provided_args) {
cli::cli_abort(
"epi_slide_opt: the argument `new_col_name` is not supported for `epi_slide_opt`. If you want to customize
the output column names, use `dplyr::rename` after the slide.",
the output column names, use `.prefix =`, `.suffix =`, or `.new_col_**names** =`.",
class = "epiprocess__epi_slide_opt__new_name_not_supported"
)
}
Expand Down
4 changes: 2 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,7 @@ time_delta_to_n_steps <- function(time_delta, time_type) {
week = "weeks",
cli_abort("difftime objects not supported for time_type {format_chr_with_quotes(time_type)}")
)
units(time_delta) <- output_units # converts number accordingly, doesn't just set attr
units(time_delta) <- output_units # converts number to represent same duration; not just attr<-
n_steps <- vec_data(time_delta)
if (!is_bare_integerish(n_steps)) {
cli_abort("`time_delta` did not appear to contain only integerish numbers
Expand All @@ -1164,7 +1164,7 @@ time_delta_to_n_steps <- function(time_delta, time_type) {
time_type_unit_abbrs <- c(
day = "d",
week = "w",
yearmon = "m"
yearmonth = "m"
)

time_type_unit_abbr <- function(time_type) {
Expand Down
31 changes: 31 additions & 0 deletions man/time_delta_to_n_steps.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions tests/testthat/test-epi_slide.R
Original file line number Diff line number Diff line change
Expand Up @@ -755,3 +755,61 @@ test_that("no dplyr warnings from selecting multiple columns", {
)
expect_equal(multi_slid_select, multi_slid)
})

test_that("epi_slide_opt output naming features", {
multi_columns <- dplyr::bind_rows(
dplyr::tibble(geo_value = "ak", time_value = test_date + 1:200, value = 1:200, value2 = -1:-200),
dplyr::tibble(geo_value = "al", time_value = test_date + 1:5, value = -(1:5), value2 = 1:5)
) %>%
as_epi_df() %>%
group_by(geo_value)
multi_columns_weekly <- dplyr::bind_rows(
dplyr::tibble(geo_value = "ak", time_value = test_date + 7*(1:200), value = 1:200, value2 = -1:-200),

Check warning on line 767 in tests/testthat/test-epi_slide.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-epi_slide.R,line=767,col=63,[infix_spaces_linter] Put spaces around all infix operators.

Check warning on line 767 in tests/testthat/test-epi_slide.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-epi_slide.R,line=767,col=64,[spaces_left_parentheses_linter] Place a space before left parenthesis, except in a function call.
dplyr::tibble(geo_value = "al", time_value = test_date + 7*(1:5), value = -(1:5), value2 = 1:5)

Check warning on line 768 in tests/testthat/test-epi_slide.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-epi_slide.R,line=768,col=63,[infix_spaces_linter] Put spaces around all infix operators.

Check warning on line 768 in tests/testthat/test-epi_slide.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-epi_slide.R,line=768,col=64,[spaces_left_parentheses_linter] Place a space before left parenthesis, except in a function call.
) %>%
as_epi_df() %>%
group_by(geo_value)
yearmonthly <-
tibble::tibble(geo_value = 1,
time_value = tsibble::make_yearmonth(2000, 1) + 1:30 - 1,
value = 1:30 %% 2 == 0) %>%
as_epi_df() %>%
group_by(geo_value)

# Auto-naming:
# * Changing .f and .window_size:
expect_equal(
multi_columns %>% epi_slide_opt(value2, frollmean, .window_size = 14) %>% names(),
c(names(multi_columns), "value2_14dav")
)
expect_equal(
multi_columns %>% epi_slide_opt(value2, slide_mean, .window_size = as.difftime(14, units = "days")) %>% names(),
c(names(multi_columns), "value2_14dav")
)
expect_equal(
multi_columns %>% epi_slide_opt(value2, slide_sum, .window_size = Inf) %>% names(),
c(names(multi_columns), "value2_running_sum")
)
# * Changing .f and .align:
expect_equal(
multi_columns %>% epi_slide_opt(value2, slide_min, .window_size = 14, .align = "center") %>% names(),
c(names(multi_columns), "value2_14dcmin")
)
expect_equal(
multi_columns %>% epi_slide_opt(value2, slide_max, .window_size = 14, .align = "left") %>% names(),
c(names(multi_columns), "value2_14dlmax")
)
# * Changing .f, time_type, .window_size:
expect_equal(
multi_columns_weekly %>% epi_slide_opt(value2, slide_prod, .window_size = 2) %>% names(),
c(names(multi_columns_weekly), "value2_2wprod")
)
expect_equal(
multi_columns_weekly %>% epi_slide_opt(value2, slide_prod, .window_size = as.difftime(2, units = "weeks")) %>% names(),

Check warning on line 808 in tests/testthat/test-epi_slide.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-epi_slide.R,line=808,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 123 characters.
c(names(multi_columns_weekly), "value2_2wprod")
)
expect_equal(
yearmonthly %>% epi_slide_opt(value, slide_any, .window_size = 3) %>% names(),
c(names(yearmonthly), "value_3many") # not the best name, but super unlikely anyway
)
})

0 comments on commit 6db1391

Please sign in to comment.