Skip to content

Commit e762e5e

Browse files
committed
refactor(model): move valid_inputs() and related functions to own R script
1 parent e68725e commit e762e5e

File tree

7 files changed

+104
-99
lines changed

7 files changed

+104
-99
lines changed

R/model.R

Lines changed: 1 addition & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -83,99 +83,6 @@ model <- function(run_number, param, set_seed = TRUE) {
8383
now(env) - .data[["start_time"]],
8484
NA))
8585
}
86-
return(result)
87-
}
88-
89-
#' Validate input parameters for the simulation.
90-
#'
91-
#' @param run_number Integer representing index of current simulation run.
92-
#' @param param List containing parameters for the simulation.
93-
#'
94-
#' @return Throws an error if any parameter is invalid.
95-
#' @export
96-
97-
valid_inputs <- function(run_number, param) {
98-
check_run_number(run_number)
99-
check_param_names(param)
100-
check_param_values(param)
101-
}
102-
103-
#' Checks if the run number is a non-negative integer.
104-
#'
105-
#' @param run_number Integer representing index of current simulation run.
106-
#'
107-
#' @return Throws an error if the run number is invalid.
108-
109-
check_run_number <- function(run_number) {
110-
if (run_number < 0L || run_number %% 1L != 0L) {
111-
stop("The run number must be a non-negative integer. Provided: ",
112-
run_number, call. = FALSE)
113-
}
114-
}
115-
116-
#' Validate parameter names.
117-
#'
118-
#' Ensure that all required parameters are present, and no extra parameters are
119-
#' provided.
120-
#'
121-
#' @param param List containing parameters for the simulation.
122-
#'
123-
#' @return Throws an error if there are missing or extra parameters.
12486

125-
check_param_names <- function(param) {
126-
# Get valid argument names from the function
127-
valid_names <- names(formals(parameters))
128-
129-
# Get names from input parameter list
130-
input_names <- names(param)
131-
132-
# Find missing keys (i.e. are there things in valid_names not in input)
133-
# and extra keys (i.e. are there things in input not in valid_names)
134-
missing_keys <- setdiff(valid_names, input_names)
135-
extra_keys <- setdiff(input_names, valid_names)
136-
137-
# If there are any missing or extra keys, throw an error
138-
if (length(missing_keys) > 0L || length(extra_keys) > 0L) {
139-
error_message <- ""
140-
if (length(missing_keys) > 0L) {
141-
error_message <- paste0(
142-
error_message, "Missing keys: ", toString(missing_keys), ". "
143-
)
144-
}
145-
if (length(extra_keys) > 0L) {
146-
error_message <- paste0(
147-
error_message, "Extra keys: ", toString(extra_keys), ". "
148-
)
149-
}
150-
stop(error_message, call. = FALSE)
151-
}
152-
}
153-
154-
#' Validate parameter values.
155-
#'
156-
#' Ensure that specific parameters are positive numbers, or non-negative
157-
#' integers.
158-
#'
159-
#' @param param List containing parameters for the simulation.
160-
#'
161-
#' @return Throws an error if any specified parameter value is invalid.
162-
163-
check_param_values <- function(param) {
164-
165-
# Check that listed parameters are always positive
166-
p_list <- c("patient_inter", "mean_n_consult_time", "number_of_runs")
167-
for (p in p_list) {
168-
if (param[[p]] <= 0L) {
169-
stop('The parameter "', p, '" must be greater than 0.', call. = FALSE)
170-
}
171-
}
172-
173-
# Check that listed parameters are non-negative integers
174-
n_list <- c("data_collection_period", "number_of_nurses")
175-
for (n in n_list) {
176-
if (param[[n]] < 0L || param[[n]] %% 1L != 0L) {
177-
stop('The parameter "', n,
178-
'" must be an integer greater than or equal to 0.', call. = FALSE)
179-
}
180-
}
87+
return(result)
18188
}

R/validate_model_inputs.R

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#' Validate input parameters for the simulation.
2+
#'
3+
#' @param run_number Integer representing index of current simulation run.
4+
#' @param param List containing parameters for the simulation.
5+
#'
6+
#' @return Throws an error if any parameter is invalid.
7+
#' @export
8+
9+
valid_inputs <- function(run_number, param) {
10+
check_run_number(run_number)
11+
check_param_names(param)
12+
check_param_values(param)
13+
}
14+
15+
16+
#' Checks if the run number is a non-negative integer.
17+
#'
18+
#' @param run_number Integer representing index of current simulation run.
19+
#'
20+
#' @return Throws an error if the run number is invalid.
21+
22+
check_run_number <- function(run_number) {
23+
if (run_number < 0L || run_number %% 1L != 0L) {
24+
stop("The run number must be a non-negative integer. Provided: ",
25+
run_number, call. = FALSE)
26+
}
27+
}
28+
29+
30+
#' Validate parameter names.
31+
#'
32+
#' Ensure that all required parameters are present, and no extra parameters are
33+
#' provided.
34+
#'
35+
#' @param param List containing parameters for the simulation.
36+
#'
37+
#' @return Throws an error if there are missing or extra parameters.
38+
39+
check_param_names <- function(param) {
40+
# Get valid argument names from the function
41+
valid_names <- names(formals(parameters))
42+
43+
# Get names from input parameter list
44+
input_names <- names(param)
45+
46+
# Find missing keys (i.e. are there things in valid_names not in input)
47+
# and extra keys (i.e. are there things in input not in valid_names)
48+
missing_keys <- setdiff(valid_names, input_names)
49+
extra_keys <- setdiff(input_names, valid_names)
50+
51+
# If there are any missing or extra keys, throw an error
52+
if (length(missing_keys) > 0L || length(extra_keys) > 0L) {
53+
error_message <- ""
54+
if (length(missing_keys) > 0L) {
55+
error_message <- paste0(
56+
error_message, "Missing keys: ", toString(missing_keys), ". "
57+
)
58+
}
59+
if (length(extra_keys) > 0L) {
60+
error_message <- paste0(
61+
error_message, "Extra keys: ", toString(extra_keys), ". "
62+
)
63+
}
64+
stop(error_message, call. = FALSE)
65+
}
66+
}
67+
68+
69+
#' Validate parameter values.
70+
#'
71+
#' Ensure that specific parameters are positive numbers, or non-negative
72+
#' integers.
73+
#'
74+
#' @param param List containing parameters for the simulation.
75+
#'
76+
#' @return Throws an error if any specified parameter value is invalid.
77+
78+
check_param_values <- function(param) {
79+
80+
# Check that listed parameters are always positive
81+
p_list <- c("patient_inter", "mean_n_consult_time", "number_of_runs")
82+
for (p in p_list) {
83+
if (param[[p]] <= 0L) {
84+
stop('The parameter "', p, '" must be greater than 0.', call. = FALSE)
85+
}
86+
}
87+
88+
# Check that listed parameters are non-negative integers
89+
n_list <- c("data_collection_period", "number_of_nurses")
90+
for (n in n_list) {
91+
if (param[[n]] < 0L || param[[n]] %% 1L != 0L) {
92+
stop('The parameter "', n,
93+
'" must be an integer greater than or equal to 0.', call. = FALSE)
94+
}
95+
}
96+
}

man/check_param_names.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.

man/check_param_values.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.

man/check_run_number.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.

man/get_run_results.Rd

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

man/valid_inputs.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.

0 commit comments

Comments
 (0)