Skip to content

Commit bf4b679

Browse files
authored
Porting add_param and rm_globalevent (#47)
* Adding missing ports * Removing vscode settings * Removing .vscode from repo
1 parent ad6aca2 commit bf4b679

11 files changed

+109
-109
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ docs
2323

2424
config.log
2525
config.status
26+
27+
.vscode

.vscode/c_cpp_properties.json

Lines changed: 0 additions & 20 deletions
This file was deleted.

.vscode/settings.json

Lines changed: 0 additions & 66 deletions
This file was deleted.

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
S3method("[",epiworld_agents)
44
S3method("[",epiworld_entities)
5+
S3method(add_param,epiworld_model)
56
S3method(add_tool,epiworld_model)
67
S3method(add_virus,epiworld_model)
78
S3method(add_virus,epiworld_seir)
@@ -108,6 +109,7 @@ export(ModelSISD)
108109
export(ModelSURV)
109110
export(add_entity)
110111
export(add_globalevent)
112+
export(add_param)
111113
export(add_tool)
112114
export(add_tool_agent)
113115
export(add_tool_n)
@@ -175,6 +177,7 @@ export(plot_reproductive_number)
175177
export(queuing_off)
176178
export(queuing_on)
177179
export(rm_entity)
180+
export(rm_globalevent)
178181
export(rm_tool)
179182
export(rm_virus)
180183
export(run)

R/cpp11.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,10 @@ set_param_cpp <- function(model, pname, val) {
308308
.Call(`_epiworldR_set_param_cpp`, model, pname, val)
309309
}
310310

311+
add_param_cpp <- function(model, pname, val) {
312+
.Call(`_epiworldR_add_param_cpp`, model, pname, val)
313+
}
314+
311315
set_name_cpp <- function(model, mname) {
312316
.Call(`_epiworldR_set_name_cpp`, model, mname)
313317
}

R/global-actions.R

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11

2-
#' Global Actions
2+
#' Global Events
33
#'
4-
#' Global actions are functions that are executed at each time step of the
4+
#' Global events are functions that are executed at each time step of the
55
#' simulation. They are useful for implementing interventions, such as
66
#' vaccination, isolation, and social distancing by means of tools.
77
#'
88
#' @export
99
#' @param prob Numeric scalar. A probability between 0 and 1.
1010
#' @param tool An object of class [tool].
11-
#' @name global-actions
11+
#' @name global-events
12+
#' @aliases global-actions
1213
#' @examples
1314
#' # Simple model
1415
#' model_sirconn <- ModelSIRCONN(
@@ -32,7 +33,7 @@
3233
#' )
3334
#'
3435
#'
35-
#' # Adding a global action
36+
#' # Adding a global event
3637
#' vaccine_day_20 <- globalevent_tool(epitool, .2, day = 20)
3738
#' add_globalevent(model_sirconn, vaccine_day_20)
3839
#'
@@ -98,7 +99,7 @@ globalaction_tool <- function(...) {
9899
}
99100

100101
#' @export
101-
#' @rdname global-actions
102+
#' @rdname global-events
102103
#' @param vars Integer vector. The position of the variables in the model.
103104
#' @param coefs Numeric vector. The coefficients of the logistic regression.
104105
#' @details The function `globalevent_tool_logit` allows to specify a logistic
@@ -143,7 +144,7 @@ globalaction_tool_logit <- function(...) {
143144
#' @export
144145
#' @param param Character scalar. The name of the parameter to be set.
145146
#' @param value Numeric scalar. The value of the parameter.
146-
#' @rdname global-actions
147+
#' @rdname global-events
147148
#' @details The function `globalevent_set_param` allows to set a parameter of
148149
#' the model. The parameter is specified by its name `param` and the value by
149150
#' `value`.
@@ -180,7 +181,7 @@ globalaction_set_params <- function(...) {
180181
}
181182

182183
#' @export
183-
#' @rdname global-actions
184+
#' @rdname global-events
184185
#' @param fun Function. The function to be executed.
185186
#' @details The function `globalevent_fun` allows to specify a function to be
186187
#' executed at a given day. The function object must receive an object of class
@@ -258,11 +259,15 @@ print.epiworld_globalevent <- function(x, ...) {
258259
}
259260

260261
#' @export
261-
#' @param action A global action.
262+
#' @param action (Deprecated) use `event` instead.
263+
#' @param event The event to be added or removed. If it is to add, then
264+
#' it should be an object of class `epiworld_globalevent`. If it is to remove,
265+
#' it should be an integer with the position of the event in the model
266+
#' (starting from zero).
262267
#' @param day Integer. The day (step) at which the action is executed (see details).
263268
#' @param model An object of class [epiworld_model].
264269
#' @param name Character scalar. The name of the action.
265-
#' @rdname global-actions
270+
#' @rdname global-events
266271
#' @seealso epiworld-model
267272
#' @details The function `add_globalevent` adds a global action to a model.
268273
#' The model checks for actions to be executed at each time step. If the added
@@ -271,12 +276,31 @@ print.epiworld_globalevent <- function(x, ...) {
271276
#' the action is executed at the specified time step.
272277
#' @returns
273278
#' - The function `add_globalevent` returns the model with the added
274-
#' action.
275-
add_globalevent <- function(model, action) {
279+
#' event
280+
add_globalevent <- function(model, event, action = NULL) {
276281

277-
if (length(attr(action, "tool")))
278-
add_tool(model, attr(action, "tool"))
282+
if (missing(event) && !missing(action)) {
283+
event <- action
284+
warning("The argument `action` is deprecated. Use `event` instead.")
285+
}
286+
287+
stopifnot_model(model)
288+
289+
if (length(attr(event, "tool")))
290+
add_tool(model, attr(event, "tool"))
291+
292+
invisible(add_globalevent_cpp(model, event))
293+
294+
}
295+
296+
#' @export
297+
#' @rdname global-events
298+
#' @returns
299+
#' - The function `rm_globalevent` returns the model with the removed
300+
#' event.
301+
rm_globalevent <- function(model, event) {
279302

280-
invisible(add_globalevent_cpp(model, action))
303+
stopifnot_model(model)
304+
invisible(rm_globalevent_cpp(model, event))
281305

282306
}

R/model-methods.R

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,19 @@ get_param.epiworld_model <- function(x, pname) {
210210
}
211211

212212

213+
#' @export
214+
#' @rdname epiworld-methods
215+
#' @returns
216+
#' - `add_param` returns the model with the added parameter invisibly.
217+
add_param <- function(x, pname, pval) UseMethod("add_param")
218+
219+
#' @export
220+
#' @rdname epiworld-methods
221+
add_param.epiworld_model <- function(x, pname, pval) {
222+
invisible(add_param_cpp(x, pname, pval))
223+
}
224+
225+
213226
#' @export
214227
#' @param pval Numeric. Value of the parameter.
215228
#' @returns
@@ -221,7 +234,6 @@ set_param <- function(x, pname, pval) UseMethod("set_param")
221234
#' @export
222235
set_param.epiworld_model <- function(x, pname, pval) {
223236
invisible(set_param_cpp(x, pname, pval))
224-
invisible(x)
225237
}
226238

227239
#' @export

man/epiworld-methods.Rd

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

man/global-actions.Rd renamed to man/global-events.Rd

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

src/cpp11.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,13 @@ extern "C" SEXP _epiworldR_set_param_cpp(SEXP model, SEXP pname, SEXP val) {
545545
END_CPP11
546546
}
547547
// model.cpp
548+
SEXP add_param_cpp(SEXP model, std::string pname, double val);
549+
extern "C" SEXP _epiworldR_add_param_cpp(SEXP model, SEXP pname, SEXP val) {
550+
BEGIN_CPP11
551+
return cpp11::as_sexp(add_param_cpp(cpp11::as_cpp<cpp11::decay_t<SEXP>>(model), cpp11::as_cpp<cpp11::decay_t<std::string>>(pname), cpp11::as_cpp<cpp11::decay_t<double>>(val)));
552+
END_CPP11
553+
}
554+
// model.cpp
548555
SEXP set_name_cpp(SEXP model, std::string mname);
549556
extern "C" SEXP _epiworldR_set_name_cpp(SEXP model, SEXP mname) {
550557
BEGIN_CPP11
@@ -1020,6 +1027,7 @@ static const R_CallMethodDef CallEntries[] = {
10201027
{"_epiworldR_ModelSURV_cpp", (DL_FUNC) &_epiworldR_ModelSURV_cpp, 13},
10211028
{"_epiworldR_add_entity_cpp", (DL_FUNC) &_epiworldR_add_entity_cpp, 2},
10221029
{"_epiworldR_add_globalevent_cpp", (DL_FUNC) &_epiworldR_add_globalevent_cpp, 2},
1030+
{"_epiworldR_add_param_cpp", (DL_FUNC) &_epiworldR_add_param_cpp, 3},
10231031
{"_epiworldR_add_tool_agent_cpp", (DL_FUNC) &_epiworldR_add_tool_agent_cpp, 5},
10241032
{"_epiworldR_add_tool_cpp", (DL_FUNC) &_epiworldR_add_tool_cpp, 2},
10251033
{"_epiworldR_add_virus_agent_cpp", (DL_FUNC) &_epiworldR_add_virus_agent_cpp, 5},

0 commit comments

Comments
 (0)