Skip to content

Commit 5ba0cd7

Browse files
committed
style(lint): linting
1 parent be5c4ed commit 5ba0cd7

14 files changed

+196
-79
lines changed

NAMESPACE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
export(add_patient_generator)
44
export(check_all_param_names)
5+
export(check_all_positive)
6+
export(check_nonneg_integer)
57
export(check_param_names)
68
export(check_param_values)
9+
export(check_positive_integer)
710
export(check_prob_vector)
811
export(check_run_number)
912
export(create_asu_arrivals)
@@ -15,6 +18,7 @@ export(create_rehab_arrivals)
1518
export(create_rehab_los)
1619
export(create_rehab_routing)
1720
export(create_rehab_trajectory)
21+
export(filter_warmup)
1822
export(get_occupancy_stats)
1923
export(model)
2024
export(runner)

R/filter_warmup.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ filter_warmup <- function(result, warm_up_period) {
1515
group_by(.data[["name"]]) |>
1616
filter(all(.data[["start_time"]] >= warm_up_period)) |>
1717
ungroup()
18-
result[["occupancy"]] <- result[["occupancy"]] |>
19-
filter(time >= warm_up_period)
18+
result[["occupancy"]] <- filter(result[["occupancy"]],
19+
time >= warm_up_period)
2020
}
21-
return(result)
21+
result
2222
}

R/model.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# To avoid package build warning (name in expand.grid())
2-
utils::globalVariables(c("time"))
2+
utils::globalVariables("time")
33

44
#' Run simulation.
55
#'

R/validate_model_inputs.R

Lines changed: 71 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ check_prob_vector <- function(vec, name) {
117117
if (!is.numeric(vec)) {
118118
stop('Routing vector "', name, '" must be numeric.', call. = FALSE)
119119
}
120-
if (any(vec < 0 | vec > 1)) {
120+
if (any(vec < 0L | vec > 1L)) {
121121
stop('All values in routing vector "', name, '" must be between 0 and 1.',
122122
call. = FALSE)
123123
}
@@ -128,54 +128,88 @@ check_prob_vector <- function(vec, name) {
128128
}
129129

130130

131-
#' Validate parameter values.
131+
#' Check if a value is a positive integer
132132
#'
133-
#' Ensure that specific parameters are positive numbers, or non-negative
134-
#' integers, and that probability vectors are in [0, 1] and sum to 1 (within
135-
#' tolerance).
133+
#' Throws an error if the value is not a positive integer (> 0).
136134
#'
137-
#' @param param List containing parameters for the simulation.
135+
#' @param x The value to check.
136+
#' @param name The name of the parameter (for error messages).
138137
#'
139-
#' @return Throws an error if any specified parameter value is invalid.
138+
#' @return None. Throws an error if the check fails.
140139
#' @export
141140

142-
check_param_values <- function(param) {
143-
144-
# Check that number of runs is positive integer
145-
if (
146-
!is.null("number_of_runs") &&
147-
"number_of_runs" <= 0L &&
148-
"number_of_runs" %% 1L == 0L
149-
) {
150-
stop('The parameter "number_of_runs" must be integer greater than 0.',
151-
call. = FALSE)
141+
check_positive_integer <- function(x, name) {
142+
if (is.null(x) || x <= 0L || x %% 1L != 0L) {
143+
stop(
144+
sprintf('The parameter "%s" must be an integer greater than 0.', name),
145+
call. = FALSE
146+
)
152147
}
148+
}
153149

154-
# Check that listed parameters are not negative (i.e. must be 0+)
155-
non_neg <- c("asu_arrivals", "rehab_arrivals", "asu_los", "rehab_los")
156-
for (sub in non_neg) {
157-
if (!is.null(param[[sub]])) {
158-
vals <- unlist(param[[sub]])
159-
if (any(vals <= 0L)) {
160-
stop('All values in "', sub, '" must be greater than 0.', call. = FALSE)
161-
}
162-
}
150+
#' Check if all values are positive
151+
#'
152+
#' Throws an error if any value in the input is not positive (> 0).
153+
#'
154+
#' @param x The vector or list of values to check.
155+
#' @param name The name of the parameter (for error messages).
156+
#'
157+
#' @return None. Throws an error if the check fails.
158+
#' @export
159+
160+
check_all_positive <- function(x, name) {
161+
if (!is.null(x) && any(unlist(x) <= 0L)) {
162+
stop(
163+
sprintf('All values in "%s" must be greater than 0.', name),
164+
call. = FALSE
165+
)
163166
}
167+
}
164168

165-
# Check that listed parameters are non-negative integers
166-
n_list <- c("warm_up_period", "data_collection_period")
167-
for (n in n_list) {
168-
if (param[[n]] < 0L || param[[n]] %% 1L != 0L) {
169-
stop('The parameter "', n,
170-
'" must be an integer greater than or equal to 0.', call. = FALSE)
171-
}
169+
#' Check if a value is a non-negative integer
170+
#'
171+
#' Throws an error if the value is not a non-negative integer (>= 0).
172+
#'
173+
#' @param x The value to check.
174+
#' @param name The name of the parameter (for error messages).
175+
#'
176+
#' @return None. Throws an error if the check fails.
177+
#' @export
178+
179+
check_nonneg_integer <- function(x, name) {
180+
if (is.null(x) || x < 0L || x %% 1L != 0L) {
181+
stop(
182+
sprintf('The parameter "%s" must be an integer >= 0.', name),
183+
call. = FALSE
184+
)
172185
}
186+
}
187+
188+
#' Validate parameter values
189+
#'
190+
#' Ensures that specific parameters are positive numbers, non-negative integers,
191+
#' and that probability vectors are in [0, 1] and sum to 1 (within tolerance).
192+
#'
193+
#' @param param List containing parameters for the simulation.
194+
#'
195+
#' @return None. Throws an error if any specified parameter value is invalid.
196+
#' @export
197+
198+
check_param_values <- function(param) {
199+
check_positive_integer(param[["number_of_runs"]], "number_of_runs")
200+
201+
lapply(c("asu_arrivals", "rehab_arrivals", "asu_los", "rehab_los"),
202+
function(nm) check_all_positive(param[[nm]], nm))
203+
204+
lapply(c("warm_up_period", "data_collection_period"),
205+
function(nm) check_nonneg_integer(param[[nm]], nm))
173206

174-
# Check that listed parameters are between 0 and 1 and sum to 1 (+- 0.01)
175207
for (routing in c("asu_routing", "rehab_routing")) {
176-
for (name in names(param[[routing]])) {
177-
vec <- unlist(param[[routing]][[name]], use.names = FALSE)
178-
check_prob_vector(vec, paste0(routing, "$", name))
208+
if (!is.null(param[[routing]])) {
209+
lapply(names(param[[routing]]), function(name) {
210+
vec <- unlist(param[[routing]][[name]], use.names = FALSE)
211+
check_prob_vector(vec, paste0(routing, "$", name))
212+
})
179213
}
180214
}
181215
}

man/check_all_positive.Rd

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

man/check_nonneg_integer.Rd

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

man/check_param_values.Rd

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

man/check_positive_integer.Rd

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

man/filter_warmup.Rd

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

rmarkdown/analysis.Rmd

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -302,14 +302,14 @@ make_delay_table <- function(
302302
303303
# Extract and filter base and scenario data
304304
base_df <- base[[unit_name]] |>
305-
filter(beds %in% unit_beds) |>
306-
select(beds, .data[["prob_delay"]], .data[["1_in_n_delay"]]) |>
305+
filter(.data[["beds"]] %in% unit_beds) |>
306+
select(.data[["beds"]], .data[["prob_delay"]], .data[["1_in_n_delay"]]) |>
307307
rename_with(~paste0(., "_", base_name),
308308
c("prob_delay", "1_in_n_delay"))
309309
310310
scenario_df <- scenario[[unit_name]] |>
311-
filter(beds %in% unit_beds) |>
312-
select(beds, .data[["prob_delay"]], .data[["1_in_n_delay"]]) |>
311+
filter(.data[["beds"]] %in% unit_beds) |>
312+
select(.data[["beds"]], .data[["prob_delay"]], .data[["1_in_n_delay"]]) |>
313313
rename_with(~paste0(., "_", scenario_name),
314314
c("prob_delay", "1_in_n_delay"))
315315
@@ -325,7 +325,7 @@ make_delay_table <- function(
325325
326326
# Combine and return
327327
bind_rows(asu_table, rehab_table) |>
328-
arrange(unit, beds)
328+
arrange(unit, .data[["beds"]])
329329
}
330330
```
331331

0 commit comments

Comments
 (0)