Skip to content

Commit 7fd380c

Browse files
authored
Merge pull request #500 from cmu-delphi/optAllOfFix
adding explicit columns for `epi_slide_opt`'s multi column case
2 parents 1f44295 + 439906b commit 7fd380c

10 files changed

+56
-26
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: epiprocess
33
Title: Tools for basic signal processing in epidemiology
4-
Version: 0.8.3
4+
Version: 0.8.4
55
Authors@R: c(
66
person("Jacob", "Bien", role = "ctb"),
77
person("Logan", "Brooks", email = "[email protected]", role = c("aut", "cre")),

NEWS.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Pre-1.0.0 numbering scheme: 0.x will indicate releases, while 0.x.y will indicat
1313
## Bug fixes
1414

1515
- Fix `epi_slide_opt` (and related functions) to correctly handle `before=Inf`.
16+
Also allow multiple columns specified as a list of strings.
1617
- Disallow `after=Inf` in slide functions, since it doesn't seem like a likely
1718
use case and complicates code.
1819

R/slide.R

+7-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
#'
4040
#' @importFrom lubridate days weeks
4141
#' @importFrom dplyr bind_rows group_vars filter select
42-
#' @importFrom rlang .data .env !! enquo enquos sym env missing_arg
42+
#' @importFrom rlang .data .env !! enquos sym env missing_arg
4343
#' @export
4444
#' @seealso [`epi_slide_opt`] [`epi_slide_mean`] [`epi_slide_sum`]
4545
#' @examples
@@ -327,7 +327,7 @@ epi_slide <- function(x, f, ..., before = NULL, after = NULL, ref_time_values =
327327
#'
328328
#' @template opt-slide-details
329329
#'
330-
#' @importFrom dplyr bind_rows mutate %>% arrange tibble select
330+
#' @importFrom dplyr bind_rows mutate %>% arrange tibble select all_of
331331
#' @importFrom rlang enquo quo_get_expr as_label expr_label caller_arg
332332
#' @importFrom tidyselect eval_select
333333
#' @importFrom purrr map map_lgl
@@ -509,7 +509,11 @@ epi_slide_opt <- function(x, col_names, f, ..., before = NULL, after = NULL, ref
509509
# positions of user-provided `col_names` into string column names. We avoid
510510
# using `names(pos)` directly for robustness and in case we later want to
511511
# allow users to rename fields via tidyselection.
512-
pos <- eval_select(rlang::enquo(col_names), data = x, allow_rename = FALSE)
512+
if (class(quo_get_expr(enquo(col_names))) == "character") {
513+
pos <- eval_select(all_of(col_names), data = x, allow_rename = FALSE)
514+
} else {
515+
pos <- eval_select(enquo(col_names), data = x, allow_rename = FALSE)
516+
}
513517
col_names_chr <- names(x)[pos]
514518
# Always rename results to "slide_value_<original column name>".
515519
result_col_names <- paste0("slide_value_", col_names_chr)

man-roxygen/opt-slide-params.R

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#' @param col_names <[`tidy-select`][dplyr_tidy_select]> An unquoted column
2-
#' name(e.g., `cases`), multiple column names (e.g., `c(cases, deaths)`), or
3-
#' [other tidy-select expression][tidyselect::language]. Variable names can
4-
#' be used as if they were positions in the data frame, so expressions like
5-
#' `x:y` can be used to select a range of variables. If you have the desired
6-
#' column names stored in a vector `vars`, use `col_names = all_of(vars)`.
2+
#' name(e.g., `cases`), multiple column names (e.g., `c(cases, deaths)`),
3+
#' [other tidy-select expression][tidyselect::language], or a vector of
4+
#' characters (e.g. `c("cases", "deaths")`). Variable names can be used as if
5+
#' they were positions in the data frame, so expressions like `x:y` can be
6+
#' used to select a range of variables.
77
#'
88
#' The tidy-selection renaming interface is not supported, and cannot be used
99
#' to provide output column names; if you want to customize the output column

man/epi_slide_mean.Rd

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/epi_slide_opt.Rd

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/epi_slide_sum.Rd

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-epi_slide.R

+25
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,31 @@ test_that("`epi_slide_opt` errors when passed non-`data.table`, non-`slider` fun
12741274
)
12751275
})
12761276

1277+
multi_columns <- dplyr::bind_rows(
1278+
dplyr::tibble(geo_value = "ak", time_value = test_date + 1:200, value = 1:200, value2 = -1:-200),
1279+
dplyr::tibble(geo_value = "al", time_value = test_date + 1:5, value = -(1:5), value2 = 1:5)
1280+
) %>%
1281+
as_epi_df() %>%
1282+
group_by(geo_value)
1283+
1284+
test_that("no dplyr warnings from selecting multiple columns", {
1285+
expect_no_warning(
1286+
multi_slid <- epi_slide_mean(multi_columns, col_names = c("value", "value2"), before = 3L)
1287+
)
1288+
expect_equal(
1289+
names(multi_slid),
1290+
c("geo_value", "time_value", "value", "value2", "slide_value_value", "slide_value_value2")
1291+
)
1292+
expect_no_warning(
1293+
multi_slid_select <- epi_slide_mean(multi_columns, c(value, value2), before = 3L)
1294+
)
1295+
expect_equal(multi_slid_select, multi_slid)
1296+
expect_no_warning(
1297+
multi_slid_select <- epi_slide_mean(multi_columns, starts_with("value"), before = 3L)
1298+
)
1299+
expect_equal(multi_slid_select, multi_slid)
1300+
})
1301+
12771302
test_that("Inf works in before/after in slide and slide_opt", {
12781303
# Daily data
12791304
df <- dplyr::bind_rows(

tests/testthat/test-epix_slide.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
library(dplyr)
1+
suppressPackageStartupMessages(library(dplyr))
22

33
test_date <- as.Date("2020-01-01")
44

tests/testthat/test-grouped_epi_archive.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
test_that("Grouping, regrouping, and ungrouping archives works as intended", {
22
# From an example:
3-
library(dplyr)
3+
suppressPackageStartupMessages(library(dplyr))
44
toy_archive <-
55
tribble(
66
~geo_value, ~age_group, ~time_value, ~version, ~value,

0 commit comments

Comments
 (0)