Skip to content

Commit f3f16e3

Browse files
committed
add tests, and style
1 parent 1e6be8f commit f3f16e3

File tree

4 files changed

+32
-15
lines changed

4 files changed

+32
-15
lines changed

R/autoplot.R

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,15 @@
3232
#' .color_by = "none",
3333
#' .facet_by = "geo_value"
3434
#' )
35-
#' autoplot(jhu_csse_daily_subset, case_rate_7d_av, .color_by = "none",
36-
#' .base_color = "red", .facet_by = "geo_value")
35+
#' autoplot(jhu_csse_daily_subset, case_rate_7d_av,
36+
#' .color_by = "none",
37+
#' .base_color = "red", .facet_by = "geo_value"
38+
#' )
3739
#'
3840
#' # .base_color specification won't have any effect due .color_by default
3941
#' autoplot(jhu_csse_daily_subset, case_rate_7d_av,
40-
#' .base_color = "red", .facet_by = "geo_value")
42+
#' .base_color = "red", .facet_by = "geo_value"
43+
#' )
4144
autoplot.epi_df <- function(
4245
object, ...,
4346
.color_by = c("all_keys", "geo_value", "other_keys", ".response", "all", "none"),
@@ -59,7 +62,8 @@ autoplot.epi_df <- function(
5962
allowed <- allowed[allowed]
6063
if (length(allowed) == 0 && rlang::dots_n(...) == 0L) {
6164
cli::cli_abort("No numeric variables were available to plot automatically.",
62-
class = "epiprocess__no_numeric_vars_available")
65+
class = "epiprocess__no_numeric_vars_available"
66+
)
6367
}
6468
vars <- tidyselect::eval_select(rlang::expr(c(...)), object)
6569
if (rlang::is_empty(vars)) { # find them automatically if unspecified
@@ -76,11 +80,13 @@ autoplot.epi_df <- function(
7680
class = "epiprocess__all_requested_vars_not_numeric"
7781
)
7882
} else if (!all(ok)) {
79-
cli::cli_warn(c(
80-
"Only the requested variables {.var {names(vars)[ok]}} are numeric.",
81-
i = "`autoplot()` cannot display {.var {names(vars)[!ok]}}."
82-
),
83-
class = "epiprocess__some_requested_vars_not_numeric")
83+
cli::cli_warn(
84+
c(
85+
"Only the requested variables {.var {names(vars)[ok]}} are numeric.",
86+
i = "`autoplot()` cannot display {.var {names(vars)[!ok]}}."
87+
),
88+
class = "epiprocess__some_requested_vars_not_numeric"
89+
)
8490
vars <- vars[ok]
8591
}
8692
}

R/epi_df.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,11 @@ as_epi_df.epi_df <- function(x, ...) {
297297
#' @export
298298
as_epi_df.tbl_df <- function(x, geo_type, time_type, as_of,
299299
additional_metadata = list(), ...) {
300-
if (!test_subset(c("geo_value", "time_value"), names(x))) cli_abort(
301-
"Columns `geo_value` and `time_value` must be present in `x`."
302-
)
300+
if (!test_subset(c("geo_value", "time_value"), names(x))) {
301+
cli_abort(
302+
"Columns `geo_value` and `time_value` must be present in `x`."
303+
)
304+
}
303305

304306
new_epi_df(
305307
x, geo_type, time_type, as_of,

R/outliers.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,11 @@ detect_outlr <- function(x = seq_along(y), y,
109109

110110
# Validate the output
111111
assert_data_frame(results)
112-
if (!test_subset(c("lower", "upper", "replacement"), colnames(results))) cli_abort(
113-
"Columns `lower`, `upper`, and `replacement` must be present in the output of the outlier detection method."
114-
)
112+
if (!test_subset(c("lower", "upper", "replacement"), colnames(results))) {
113+
cli_abort(
114+
"Columns `lower`, `upper`, and `replacement` must be present in the output of the outlier detection method."
115+
)
116+
}
115117

116118
# Update column names with model abbreviation
117119
colnames(results) <- paste(abbr, colnames(results), sep = "_")

tests/testthat/test-utils.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ test_that("guess_time_type works for different types", {
8989
expect_equal(guess_time_type(not_ymd3), "custom")
9090
expect_equal(guess_time_type(not_a_date), "custom")
9191
})
92+
3
93+
test_that("guess_time_type works with gaps", {
94+
days_gaps <- as.Date("2022-01-01") + c(0, 1, 3, 4, 8, 8 + 7)
95+
weeks_gaps <- as.Date("2022-01-01") + 7 * c(0, 1, 3, 4, 8, 8 + 7)
96+
expect_equal(guess_time_type(days_gaps), "day")
97+
expect_equal(guess_time_type(weeks_gaps), "week")
98+
})
9299

93100
test_that("enlist works", {
94101
my_list <- enlist(x = 1, y = 2, z = 3)

0 commit comments

Comments
 (0)