@@ -829,7 +829,7 @@ The prevention of new fields is thanks to the default `lock_objects = TRUE` sett
829829#' @field transfer_prob Numeric. Transfer probability (0-1).
830830
831831ParamClass <- R6Class( # nolint: object_name_linter
832- lock_objects = FALSE,
832+ lock_objects = FALSE,
833833 public = list(
834834 transfer_prob = NULL,
835835
@@ -851,7 +851,7 @@ params <- ParamClass$new()
851851try({
852852 params$transfer_probs <- 0.4
853853})
854- params
854+ params
855855```
856856
857857#### Setting parameters within a list
@@ -867,15 +867,15 @@ You may choose to store parameters in a list instead, to make it easier to acces
867867
868868ParamClass <- R6Class( # nolint: object_name_linter
869869 public = list(
870- parameters = NULL,
870+ parameters = NULL,
871871
872872 #' @description
873873 #' Initialises the R6 object.
874874
875875 initialize = function(transfer_prob = 0.3) {
876- self$parameters <- list(
876+ self$parameters <- list(
877877 transfer_prob = transfer_prob
878- )
878+ )
879879 }
880880 )
881881)
@@ -889,7 +889,7 @@ params <- ParamClass$new()
889889try({
890890 params$parameters$transfer_probs <- 0.4
891891})
892- params$parameters
892+ params$parameters
893893```
894894
895895<br >
@@ -912,20 +912,20 @@ ParamClass <- R6Class( # nolint: object_name_linter
912912 self$transfer_prob <- transfer_prob
913913 },
914914
915- #' @description
916- #' Returns parameters as a named list.
917-
918- get_params = function() {
919- # Get all non-function fields
920- all_names <- ls(self)
921- is_not_function <- vapply(
922- all_names,
923- function(x) !is.function(self[[x]]),
924- FUN.VALUE = logical(1L)
925- )
926- param_names <- all_names[is_not_function]
927- mget(param_names, envir = self)
928- }
915+ #' @description
916+ #' Returns parameters as a named list.
917+
918+ get_params = function() {
919+ # Get all non-function fields
920+ all_names <- ls(self)
921+ is_not_function <- vapply(
922+ all_names,
923+ function(x) !is.function(self[[x]]),
924+ FUN.VALUE = logical(1L)
925+ )
926+ param_names <- all_names[is_not_function]
927+ mget(param_names, envir = self)
928+ }
929929 )
930930)
931931```
@@ -939,8 +939,8 @@ try({
939939 params$transfer_probs <- 0.4
940940})
941941
942- # Get all parameters
943- params$get_params()
942+ # Get all parameters
943+ params$get_params()
944944```
945945
946946::::
@@ -1055,7 +1055,7 @@ model <- function(param) {
10551055
10561056 # Simulation code...
10571057
1058- print ("The simulation has run!")
1058+ cat ("The simulation has run!")
10591059}
10601060
10611061
@@ -1180,26 +1180,26 @@ ParamClass <- R6Class( # nolint: object_name_linter
11801180 self$transfer_prob <- transfer_prob
11811181 },
11821182
1183- #' @description
1184- #' Check that transfer_prob is between 0 and 1.
1185- #' @return No return value; throws an error if invalid.
1186-
1187- validate_param = function() {
1188- if (self$transfer_prob < 0L || self$transfer_prob > 1L) {
1189- stop(
1190- "transfer_prob must be between 0 and 1, but is: ",
1191- self$transfer_prob, call. = FALSE
1192- )
1193- }
1194- }
1183+ #' @description
1184+ #' Check that transfer_prob is between 0 and 1.
1185+ #' @return No return value; throws an error if invalid.
1186+
1187+ validate_param = function() {
1188+ if (self$transfer_prob < 0L || self$transfer_prob > 1L) {
1189+ stop(
1190+ "transfer_prob must be between 0 and 1, but is: ",
1191+ self$transfer_prob, call. = FALSE
1192+ )
1193+ }
1194+ }
11951195 )
11961196)
11971197```
11981198
11991199``` {r}
12001200# Create instance with invalid transfer_prob and run method
12011201param <- ParamClass$new(transfer_prob = 1.4)
1202- try(param$validate_param())
1202+ try(param$validate_param())
12031203```
12041204
12051205:::
@@ -1287,4 +1287,4 @@ If you haven't already, now's the time to try out **parameter validation** in pr
12871287
12881288* Repeat this task, but with an ** invalid value** for a parameter (e.g. outside the expected range).
12891289
1290- <br ><br >
1290+ <br ><br >
0 commit comments