Skip to content

Commit

Permalink
feat: add fill argument to incidence() for use when complete_dates TRUE
Browse files Browse the repository at this point in the history
  • Loading branch information
TimTaylor committed Jun 7, 2024
1 parent 01224ec commit 1eee115
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 5 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,6 @@ importFrom(tidyr,unnest)
importFrom(tidyr,unpack)
importFrom(ympes,assert_bool)
importFrom(ympes,assert_scalar_character)
importFrom(ympes,assert_scalar_numeric)
importFrom(ympes,assert_scalar_numeric_not_na)
importFrom(ympes,assert_scalar_whole)
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# incidence2 (development version)

- `incidence()` gains a `fill` parameter. This is passed to `complete_dates()`
so is only valid when the argument `complete_dates = TRUE`.

- For convenience we now reexport `select()` from
[dplyr](https://cran.r-project.org/package=dplyr) as well as
`unnest()` and `unpack()` from [tidyr](https://cran.r-project.org/package=tidyr).

# incidence2 2.3.1

- Temporarily remove the [ympes](https://cran.r-project.org/package=ympes)
Expand Down
10 changes: 10 additions & 0 deletions R/assertions.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@
)
}

# -------------------------------------------------------------------------
.assert_scalar_numeric <- function(x) {
assert_scalar_numeric(
x,
.arg = deparse(substitute(x)),
.call = sys.call(-1L),
.subclass = NULL
)
}

# -------------------------------------------------------------------------
.assert_scalar_numeric_not_na <- function(x) {
assert_scalar_numeric_not_na(
Expand Down
23 changes: 21 additions & 2 deletions R/incidence.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,23 @@
#'
#' Should the resulting object have the same range of dates for each grouping.
#'
#' Missing counts will be filled with `0L`.
#' Missing counts will be filled with `0L` unless the `fill` argument is
#' provided (and this value will take precedence).
#'
#' Will attempt to use `function(x) seq(min(x), max(x), by = 1)` on the
#' resultant date_index column to generate a complete sequence of dates.
#'
#' More flexible completion is possible by using the `complete_dates()`
#' function.
#'
#' @param fill `numeric`.
#'
#' Only applicable when `complete_dates = TRUE`.
#'
#' The value to replace missing counts caused by completing dates.
#'
#' If unset then will default to `0L`.
#'
#' @param ...
#'
#' Not currently used.
Expand Down Expand Up @@ -176,6 +185,7 @@ incidence <- function(
interval = NULL,
offset = NULL,
complete_dates = FALSE,
fill = 0L,
...
) {

Expand Down Expand Up @@ -464,7 +474,16 @@ incidence <- function(

# complete dates if flag set
# TODO - this could be more efficient but this is safest for now
if (complete_dates) complete_dates(out) else out
if (complete_dates) {
if (missing(fill))
fill <- 0L
.assert_scalar_numeric(fill)
out <- complete_dates(out, fill = fill)
} else if (!missing(fill)) {
.stop_argument("`fill` can only be given when `complete_dates = TRUE`.")
}

out
}


Expand Down
3 changes: 2 additions & 1 deletion R/incidence2-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ NULL

#' @rawNamespace import(data.table, except = c(isoweek, year))
#' @import grates
#' @importFrom ympes assert_bool assert_scalar_character assert_scalar_numeric_not_na assert_scalar_whole
#' @importFrom ympes assert_bool assert_scalar_character assert_scalar_whole
#' @importFrom ympes assert_scalar_numeric assert_scalar_numeric_not_na
NULL
12 changes: 11 additions & 1 deletion man/incidence.Rd

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

6 changes: 5 additions & 1 deletion tests/testthat/test-incidence.R
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,11 @@ test_that("miscellaneous incidence error messaging works as expected", {
fixed = TRUE
)


expect_error(
incidence(dat, c("dates","dates2"), fill = 9),
"`fill` can only be given when `complete_dates = TRUE`.",
fixed = TRUE
)


})

0 comments on commit 1eee115

Please sign in to comment.