Skip to content

Commit a55874e

Browse files
committed
Pre-Release 0.5.1RC1
1 parent 81d3933 commit a55874e

File tree

5 files changed

+50
-43
lines changed

5 files changed

+50
-43
lines changed

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# ggsurveillance 0.5.1
22

3-
- Bug fix for corner cases for the `fill_gaps` option in `bin_by_date()`
3+
- Bug fix: Fix corner cases for the `fill_gaps` option in `bin_by_date()`
44

55
# ggsurveillance 0.5.0
66

R/bin_by_date.R

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ bin_by_date <- function(
143143
df_full_dates <- x |>
144144
dplyr::select({{ dates_from }}) |>
145145
.fill_date_gaps(date_resolution = date_resolution)
146-
146+
147147
# Join with data and create observations with weight 0
148148
suppressMessages(x <- x |>
149149
dplyr::full_join(df_full_dates) |>
@@ -180,20 +180,21 @@ bin_by_date <- function(
180180

181181
# Internal utility function for filling date gaps
182182
.fill_date_gaps <- function(dates, date_resolution = "week") {
183-
184183
suppressMessages(group_cols <- dplyr::group_keys(dates))
185-
184+
186185
# Filter out rows with NA dates
187186
clean_dates <- dates |>
188187
dplyr::ungroup() |>
189188
dplyr::select(-colnames(group_cols)) |>
190189
tidyr::drop_na()
191190

192-
if (nrow(clean_dates) == 0) return(dates)
193-
191+
if (nrow(clean_dates) == 0) {
192+
return(dates)
193+
}
194+
194195
date_col_name <- colnames(clean_dates)[1]
195196
date_vector <- clean_dates[[1]]
196-
197+
197198
# Convert lubridate units to seq() compatible units - simple mapping
198199
seq_by <- dplyr::case_when(
199200
grepl("sec", date_resolution) & inherits(date_vector, c("POSIXt")) ~ "sec",
@@ -203,19 +204,19 @@ bin_by_date <- function(
203204
grepl("week", date_resolution) ~ "week",
204205
# Use week since month can skip February (irregular intervals)
205206
grepl("month", date_resolution) ~ "week",
206-
date_resolution == "bimonth" ~ "month", # Map to base unit
207-
date_resolution == "quarter" ~ "month", # Map to base unit
208-
date_resolution == "season" ~ "month", # Map to base unit
209-
date_resolution == "halfyear" ~ "month", # Map to base unit
207+
date_resolution == "bimonth" ~ "month", # Map to base unit
208+
date_resolution == "quarter" ~ "month", # Map to base unit
209+
date_resolution == "season" ~ "month", # Map to base unit
210+
date_resolution == "halfyear" ~ "month", # Map to base unit
210211
# Use month since epi/isoyear often has only 364 days (52*7)
211-
grepl("year", date_resolution) ~ "month",
212-
TRUE ~ "day" # default
212+
grepl("year", date_resolution) ~ "month",
213+
TRUE ~ "day" # default
213214
)
214-
215+
215216
# Get date range
216217
min_date <- min(date_vector, na.rm = TRUE)
217218
max_date <- max(date_vector, na.rm = TRUE)
218-
219+
219220
# Generate complete sequence based on date type
220221
# TODO: Is this needed?
221222
if (inherits(date_vector, "Date")) {
@@ -227,7 +228,9 @@ bin_by_date <- function(
227228
}
228229

229230
# Create result data frame
230-
result <- group_cols |> dplyr::group_by_all() |> dplyr::reframe(!!date_col_name := full_sequence)
231-
231+
result <- group_cols |>
232+
dplyr::group_by_all() |>
233+
dplyr::reframe(!!date_col_name := full_sequence)
234+
232235
return(result)
233-
}
236+
}

R/geom_epicurve.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
#' labs(title = "Epicurve Example") +
7777
#' scale_y_cases_5er() +
7878
#' # Correct ISOWeek labels for week-year
79-
#' scale_x_date(date_breaks = "4 weeks", date_labels = "W%V'%g") +
79+
#' scale_x_date(date_breaks = "4 weeks", date_labels = "W%V'%g") +
8080
#' coord_equal(ratio = 7) + # Use coord_equal for square boxes. 'ratio' are the days per week.
8181
#' theme_bw()
8282
#'

man/geom_epicurve.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-bin_by_date.R

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -227,64 +227,68 @@ test_that("bin_by_date fill_gaps parameter works", {
227227
# Check that gaps are filled with zeros
228228
zero_rows <- result_fill[result_fill$n == 0, ]
229229
expect_identical(nrow(zero_rows), 2L)
230-
230+
231231
df2 <- data.frame(
232-
date = as.Date(c("2024-01-01", "2024-01-15", "2024-01-29")),
232+
date = as.Date(c("2024-01-01", "2024-01-15", "2024-01-29")),
233233
cases = c(1, 2, 3)
234234
)
235-
235+
236236
result_fill2 <- bin_by_date(df,
237-
dates_from = date, n = cases,
238-
date_resolution = "week", fill_gaps = TRUE
237+
dates_from = date, n = cases,
238+
date_resolution = "week", fill_gaps = TRUE
239239
)
240-
240+
241241
expect_identical(nrow(result_fill2), 5L)
242-
242+
243243
# Check that gaps are filled with zeros
244244
zero_rows <- result_fill[result_fill2$n == 0, ]
245245
expect_identical(nrow(zero_rows), 2L)
246-
246+
247247
# Check corner cases, isoyear has often 364 days
248248
res1 <- data.frame(date = c(as_date("2001-01-01"), as_date("2025-11-05"))) |>
249249
bin_by_date(dates_from = date, date_resolution = "isoyear", fill_gaps = TRUE)
250-
251-
expect_identical(res1$date, 2001:2025*1.0)
252-
250+
251+
expect_identical(res1$date, 2001:2025 * 1.0)
252+
253253
# Check corner cases, shorter months should not be skipped (February, April)
254254
res2 <- data.frame(date = c(as_date("2023-01-31"), as_date("2023-06-05"))) |>
255255
bin_by_date(dates_from = date, date_resolution = "month", fill_gaps = TRUE)
256-
257-
expect_identical(month(res2$date), 1:6*1.0)
256+
257+
expect_identical(month(res2$date), 1:6 * 1.0)
258258
})
259259

260260
test_that("bin_by_date fill_gaps works with grouped data", {
261261
# Create data with gaps within groups
262262
df <- data.frame(
263-
date = as.Date(c("2024-01-01", "2024-01-15", "2024-01-29", # Group A with gaps
264-
"2024-01-08", "2024-01-22")), # Group B with gaps
263+
date = as.Date(c(
264+
"2024-01-01", "2024-01-15", "2024-01-29", # Group A with gaps
265+
"2024-01-08", "2024-01-22"
266+
)), # Group B with gaps
265267
group = c("A", "A", "A", "B", "B"),
266268
cases = c(1, 2, 3, 4, 5)
267269
)
268-
270+
269271
# Test with grouping and fill_gaps = TRUE
270272
result_grouped_fill <- df |>
271273
dplyr::group_by(group) |>
272-
bin_by_date(dates_from = date, n = cases,
273-
date_resolution = "week", fill_gaps = TRUE, .groups = "drop_last")
274-
274+
bin_by_date(
275+
dates_from = date, n = cases,
276+
date_resolution = "week", fill_gaps = TRUE, .groups = "drop_last"
277+
)
278+
275279
# Should preserve grouping
276280
expect_true(dplyr::is_grouped_df(result_grouped_fill))
277281
expect_identical(dplyr::group_vars(result_grouped_fill), "group")
278-
expect_identical(nrow(result_grouped_fill), 2L*5L) # Both have same length
279-
282+
expect_identical(nrow(result_grouped_fill), 2L * 5L) # Both have same length
283+
280284
# Check that gaps are filled with zeros within groups
281285
zero_rows <- result_grouped_fill[result_grouped_fill$n == 0, ]
282286
expect_identical(nrow(zero_rows), 5L) # 2 gaps in A + 3 gaps in B
283-
287+
284288
# Test without fill_gaps for comparison
285289
result_grouped_no_fill <- df |>
286290
dplyr::group_by(group) |>
287291
bin_by_date(dates_from = date, n = cases, date_resolution = "week")
288-
292+
289293
expect_identical(nrow(result_grouped_no_fill), 5L) # Original data only
290294
})

0 commit comments

Comments
 (0)