Skip to content

Commit 98312f8

Browse files
authored
Merge pull request #151 from cmu-delphi/remove-flag
Remove flag
2 parents 5a42234 + 20ee9c3 commit 98312f8

21 files changed

+59
-96
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: epipredict
22
Title: Basic epidemiology forecasting methods
3-
Version: 0.0.3
3+
Version: 0.0.3.9999
44
Authors@R: c(
55
person("Daniel", "McDonald", , "[email protected]", role = c("aut", "cre")),
66
person("Ryan", "Tibshirani", , "[email protected]", role = "aut"),

R/create-layer.R

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ create_layer <- function(name = NULL, open = rlang::is_interactive()) {
2525
name <- usethis:::slug(name, "R")
2626
usethis:::check_file_name(name)
2727
path <- fs::path("R", name)
28-
if (! fs::file_exists(path)) {
29-
usethis::use_template("layer.R", save_as = path,
30-
data = list(name = layer_name), open = FALSE,
31-
package = "epipredict")
28+
if (!fs::file_exists(path)) {
29+
usethis::use_template(
30+
"layer.R", save_as = path,
31+
data = list(name = layer_name), open = FALSE,
32+
package = "epipredict"
33+
)
3234
}
3335
usethis::edit_file(usethis::proj_path(path), open = open)
3436
}

R/layer_add_forecast_date.R

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,8 @@ layer_add_forecast_date <-
6969
}
7070

7171
layer_add_forecast_date_new <- function(forecast_date, id) {
72-
arg_is_scalar(forecast_date, allow_null = TRUE)
73-
if (!is.null(forecast_date)) {
74-
forecast_date <- tryCatch(as.Date(forecast_date), error = function(e) NA)
75-
}
76-
arg_is_date(forecast_date, allow_null = TRUE)
72+
forecast_date <- arg_to_date(forecast_date, allow_null = TRUE)
7773
arg_is_chr_scalar(id)
78-
7974
layer("add_forecast_date", forecast_date = forecast_date, id = id)
8075
}
8176

R/layer_add_target_date.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
#' p2
4646
layer_add_target_date <-
4747
function(frosting, target_date = NULL, id = rand_id("add_target_date")) {
48+
target_date <- arg_to_date(target_date, allow_null = TRUE)
49+
arg_is_chr_scalar(id)
4850
add_layer(
4951
frosting,
5052
layer_add_target_date_new(

R/layer_naomit.R

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
#' were positions in the data frame, so expressions like `x:y` can
77
#' be used to select a range of variables. Typical usage is `.pred` to remove
88
#' any rows with `NA` predictions.
9-
#' @param .flag a logical to determine if the layer is added. Passed on to
10-
#' `add_layer()`. Default `TRUE`.
119
#' @param id a random id string
1210
#'
1311
#' @return an updated `frosting` postprocessor
@@ -35,14 +33,14 @@
3533
#'
3634
#' p <- predict(wf1, latest)
3735
#' p
38-
layer_naomit <- function(frosting, ..., .flag = TRUE, id = rand_id("naomit")) {
36+
layer_naomit <- function(frosting, ..., id = rand_id("naomit")) {
37+
arg_is_chr_scalar(id)
3938
add_layer(
4039
frosting,
4140
layer_naomit_new(
4241
terms = dplyr::enquos(...),
4342
id = id
44-
),
45-
flag = .flag
43+
)
4644
)
4745
}
4846

R/layer_point_from_distn.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ layer_point_from_distn <- function(frosting,
6464
type = type,
6565
name = name,
6666
id = id
67-
),
68-
flag = TRUE
67+
)
6968
)
7069
}
7170

R/layer_population_scaling.R

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@
4242
#' in the `epi_df`.
4343
#' @param suffix a character. The suffix added to the column name if
4444
#' `create_new = TRUE`. Default to "_original".
45-
#' @param .flag a logical to determine if the layer is added. Passed on to
46-
#' `add_layer()`. Default `TRUE`.
4745
#' @param id a random id string
4846
#'
4947
#' @return an updated `frosting` postprocessor
@@ -94,11 +92,10 @@ layer_population_scaling <- function(frosting,
9492
rate_rescaling = 1,
9593
create_new = TRUE,
9694
suffix = "_scaled",
97-
.flag = TRUE,
9895
id = rand_id("population_scaling")) {
9996

100-
arg_is_scalar(df_pop_col, rate_rescaling, create_new, suffix, .flag, id)
101-
arg_is_lgl(create_new, .flag)
97+
arg_is_scalar(df_pop_col, rate_rescaling, create_new, suffix, id)
98+
arg_is_lgl(create_new)
10299
arg_is_chr(df_pop_col, suffix, id)
103100
arg_is_chr(by, allow_null = TRUE)
104101
if (rate_rescaling <= 0)
@@ -115,8 +112,7 @@ layer_population_scaling <- function(frosting,
115112
create_new = create_new,
116113
suffix = suffix,
117114
id = id
118-
),
119-
flag = .flag
115+
)
120116
)
121117
}
122118

R/layer_predict.R

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
#'
1010
#' @inheritParams parsnip::predict.model_fit
1111
#' @param frosting a frosting object
12-
#' #' @param .flag a logical to determine if the layer is added. Passed on to
13-
#' `add_layer()`. Default `TRUE`.
14-
#' @param .flag a logical to determine if the layer is added. Passed on to
15-
#' `add_layer()`. Default `TRUE`.
1612
#' @param id a string identifying the layer
1713
#'
1814
#'
@@ -49,17 +45,17 @@
4945
#' p2
5046
layer_predict <-
5147
function(frosting, type = NULL, opts = list(), ...,
52-
.flag = TRUE,
5348
id = rand_id("predict_default")) {
49+
arg_is_chr_scalar(id)
50+
arg_is_chr_scalar(type, allow_null = TRUE)
5451
add_layer(
5552
frosting,
5653
layer_predict_new(
5754
type = type,
5855
opts = opts,
5956
dots_list = rlang::list2(...), # can't figure how to use this
6057
id = id
61-
),
62-
flag = .flag
58+
)
6359
)
6460
}
6561

@@ -71,8 +67,11 @@ layer_predict_new <- function(type, opts, dots_list, id) {
7167
#' @export
7268
slather.layer_predict <- function(object, components, the_fit, the_recipe, ...) {
7369

74-
components$predictions <- predict(the_fit, components$forged$predictors,
75-
type = object$type, opts = object$opts)
76-
components$predictions <- dplyr::bind_cols(components$keys, components$predictions)
70+
components$predictions <- predict(
71+
the_fit,
72+
components$forged$predictors,
73+
type = object$type, opts = object$opts)
74+
components$predictions <- dplyr::bind_cols(
75+
components$keys, components$predictions)
7776
components
7877
}

R/layer_predictive_distn.R

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
#' @param dist_type Gaussian or Student's t predictive intervals
1313
#' @param truncate Do we truncate the distribution to an interval
1414
#' @param name character. The name for the output column.
15-
#' @param .flag a logical to determine if the layer is added. Passed on to
16-
#' `add_layer()`. Default `TRUE`.
1715
#' @param id a random id string
1816
#'
1917
#' @return an updated `frosting` postprocessor with additional columns of the
@@ -44,18 +42,17 @@
4442
#' p <- predict(wf1, latest)
4543
#' p
4644
layer_predictive_distn <- function(frosting,
47-
...,
48-
dist_type = c("gaussian", "student_t"),
49-
truncate = c(-Inf, Inf),
50-
name = ".pred_distn",
51-
.flag = TRUE, # mandatory
52-
id = rand_id("predictive_distn")) {
45+
...,
46+
dist_type = c("gaussian", "student_t"),
47+
truncate = c(-Inf, Inf),
48+
name = ".pred_distn",
49+
id = rand_id("predictive_distn")) {
5350
rlang::check_dots_empty()
5451
arg_is_chr_scalar(name, id)
5552
dist_type <- match.arg(dist_type)
56-
stopifnot(length(truncate) == 2L,
57-
is.numeric(truncate),
58-
truncate[1] < truncate[2])
53+
stopifnot(
54+
length(truncate) == 2L, is.numeric(truncate), truncate[1] < truncate[2]
55+
)
5956

6057
add_layer(
6158
frosting,
@@ -64,8 +61,7 @@ layer_predictive_distn <- function(frosting,
6461
truncate = truncate,
6562
name = name,
6663
id = id
67-
),
68-
flag = .flag
64+
)
6965
)
7066
}
7167

R/layer_quantile_distn.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ layer_quantile_distn <- function(frosting,
5858
truncate = truncate,
5959
name = name,
6060
id = id
61-
),
62-
flag = TRUE
61+
)
6362
)
6463
}
6564

R/layer_residual_quantiles.R

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
#' @param by_key A character vector of keys to group the residuals by before
99
#' calculating quantiles. The default, `c()` performs no grouping.
1010
#' @param name character. The name for the output column.
11-
#' @param .flag a logical to determine if the layer is added. Passed on to
12-
#' `add_layer()`. Default `TRUE`.
1311
#' @param id a random id string
1412
#'
1513
#' @return an updated `frosting` postprocessor with additional columns of the
@@ -49,14 +47,13 @@ layer_residual_quantiles <- function(frosting, ...,
4947
symmetrize = TRUE,
5048
by_key = character(0L),
5149
name = ".pred_distn",
52-
.flag = TRUE,
5350
id = rand_id("residual_quantiles")) {
5451
rlang::check_dots_empty()
55-
arg_is_scalar(symmetrize, .flag)
52+
arg_is_scalar(symmetrize)
5653
arg_is_chr_scalar(name, id)
5754
arg_is_chr(by_key, allow_null = TRUE)
5855
arg_is_probabilities(probs)
59-
arg_is_lgl(symmetrize, .flag)
56+
arg_is_lgl(symmetrize)
6057
add_layer(
6158
frosting,
6259
layer_residual_quantiles_new(
@@ -65,8 +62,7 @@ layer_residual_quantiles <- function(frosting, ...,
6562
by_key = by_key,
6663
name = name,
6764
id = id
68-
),
69-
flag = .flag
65+
)
7066
)
7167
}
7268

R/layer_threshold_preds.R

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
#' @param upper Upper threshold for the prediction values. That is, any
1717
#' predictions that are greater than this upper bound are set to it.
1818
#' Default value is `Inf`.
19-
#' @param .flag a logical to determine if the layer is added. Passed on to
20-
#' `add_layer()`. Default `TRUE`.
2119
#' @param id a random id string
2220
#'
2321
#'
@@ -47,17 +45,18 @@
4745
#' p <- predict(wf, latest)
4846
#' p
4947
layer_threshold <-
50-
function(frosting, ..., lower = 0, upper = Inf, .flag = TRUE,
51-
id = rand_id("threshold")) {
48+
function(frosting, ..., lower = 0, upper = Inf, id = rand_id("threshold")) {
49+
arg_is_scalar(lower, upper)
50+
arg_is_chr_scalar(id)
51+
stopifnot(is.numeric(lower), is.numeric(upper), lower < upper)
5252
add_layer(
5353
frosting,
5454
layer_threshold_new(
5555
terms = dplyr::enquos(...),
5656
lower = lower,
5757
upper = upper,
5858
id = id
59-
),
60-
flag = .flag
59+
)
6160
)
6261
}
6362

R/layers.R

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
#'
33
#' @param frosting a `frosting` postprocessor
44
#' @param object a `frosting` layer
5-
#' @param flag logical to determine if the layer is added. Default `TRUE`.
65
#'
76
#' @return an updated `frosting` postprocessor
87
#' @export
9-
add_layer <- function(frosting, object, flag = TRUE) {
8+
add_layer <- function(frosting, object) {
109
validate_frosting(frosting)
1110
validate_layer(object)
1211

13-
if (flag) frosting$layers[[length(frosting$layers) + 1]] <- object
12+
frosting$layers[[length(frosting$layers) + 1]] <- object
1413

1514
frosting
1615
}

R/utils_arg.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,13 @@ arg_is_sorted = function(..., allow_null = FALSE) {
158158

159159
})
160160
}
161+
162+
163+
arg_to_date <- function(x, allow_null = FALSE, allow_na = FALSE) {
164+
arg_is_scalar(x, allow_null = allow_null, allow_na = allow_na)
165+
if (allow_null && !is.null(x)) {
166+
x <- tryCatch(as.Date(x), error = function(e) NA)
167+
}
168+
arg_is_date(x, allow_null = allow_null, allow_na = allow_na)
169+
x
170+
}

man/add_layer.Rd

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

man/layer_naomit.Rd

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

man/layer_population_scaling.Rd

Lines changed: 0 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)