Skip to content

Commit 5d15e40

Browse files
committed
style(lint): linting R code
1 parent 4dd78c4 commit 5d15e40

File tree

11 files changed

+80
-62
lines changed

11 files changed

+80
-62
lines changed

.lintr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
linters: all_linters(packages = "lintr")
1+
linters: all_linters(packages = "lintr", undesirable_function_linter = NULL)
22
encoding: "UTF-8"
33
exclusions: list("renv")

pages/inputs/input_data.qmd

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ The way you might set these up depends on whether you are allowed to share the r
247247
## 🧪 Test yourself
248248

249249
```{r, echo = FALSE}
250-
library(webexercises) # nolint: undesirable_function_linter
250+
library(webexercises) # nolint: library_call_linter
251251
```
252252

253253
:::{.callout-note}
@@ -257,7 +257,10 @@ library(webexercises) # nolint: undesirable_function_linter
257257
```{r, results="asis", echo = FALSE}
258258
cat(longmcq(c(
259259
"Do not share or describe anything.",
260-
answer = "Describe the dataset, and if possible also share a data dictionary and a synthetic dataset."
260+
answer = paste0(
261+
"Describe the dataset, and if possible also share a data dictionary and a",
262+
" synthetic dataset."
263+
)
261264
)))
262265
```
263266

@@ -283,7 +286,10 @@ cat(longmcq(c(
283286
```{r, results="asis", echo = FALSE}
284287
cat(longmcq(c(
285288
"Duplicate all simulation code across both repositories.",
286-
answer = "Develop the simulation code as a package so it can be shared across repositories without code duplication."
289+
answer = paste(
290+
"Develop the simulation code as a package so it can be shared across ",
291+
"repositories without code duplication."
292+
)
287293
)))
288294
```
289295

pages/inputs/parameters_script.qmd

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ This page helps you meet reproducibility criteria from:
3434
:::{.r-content}
3535
```{r}
3636
# Load required packages
37-
library(R6) # nolint: undesirable_function_linter
37+
library(R6)
3838
```
3939
:::
4040

@@ -1238,7 +1238,7 @@ Not applicable as the stroke example stores [parameters in a file](parameters_fi
12381238
## 🧪 Test yourself
12391239

12401240
```{r, echo = FALSE}
1241-
library(webexercises) # nolint: undesirable_function_linter
1241+
library(webexercises) # nolint: library_call_linter
12421242
```
12431243

12441244
:::{.callout-note}
@@ -1261,7 +1261,10 @@ cat(longmcq(c(
12611261

12621262
```{r, results="asis", echo = FALSE}
12631263
cat(longmcq(c(
1264-
answer = "It ensures that each scenario only uses the parameters intended for it, and avoids accidental reuse between runs.",
1264+
answer = paste0(
1265+
"It ensures that each scenario only uses the parameters intended for it, ",
1266+
"and avoids accidental reuse between runs."
1267+
),
12651268
"It always makes your code shorter.",
12661269
"It allows you to skip documenting your parameters."
12671270
)))

pages/inputs/parameters_validation.qmd

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -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
831831
ParamClass <- 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()
851851
try({
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
868868
ParamClass <- 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()
889889
try({
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
12011201
param <- 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>

pages/intro/foss.qmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ Both have extensive **documentation**, **active user communities**, and **proven
7979
## 🧪 Test yourself
8080

8181
```{r, echo = FALSE}
82-
library(webexercises) # nolint: undesirable_function_linter
82+
library(webexercises)
8383
```
8484

8585
:::{.callout-note}

pages/intro/rap.qmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ This book does both! As detailed on the [guidelines](guidelines.qmd) page:
8686
## 🧪 Test yourself
8787

8888
```{r, echo = FALSE}
89-
library(webexercises) # nolint: undesirable_function_linter
89+
library(webexercises)
9090
```
9191

9292
:::{.callout-note}

pages/model/distributions.qmd

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ On [the next page](patients.qmd), we'll learn how to add sampling to your model
353353
## 🧪 Test yourself
354354

355355
```{r, echo = FALSE}
356-
library(webexercises) # nolint: undesirable_function_linter
356+
library(webexercises) # nolint: library_call_linter
357357
```
358358

359359
:::{.callout-note}
@@ -363,7 +363,10 @@ library(webexercises) # nolint: undesirable_function_linter
363363
```{r, results="asis", echo = FALSE}
364364
cat(longmcq(c(
365365
"A source of truly unpredictable numbers from physical processes",
366-
answer = "An algorithm that produces numbers that appear random but are determined by a starting seed",
366+
answer = paste0(
367+
"An algorithm that produces numbers that appear random but are determined",
368+
" by a starting seed"
369+
),
367370
"A hardware device that measures system noise to generate randomness"
368371
)))
369372
```
@@ -392,7 +395,9 @@ cat(longmcq(c(
392395
cat(longmcq(c(
393396
"To make the simulation run faster",
394397
"To avoid requiring a seed",
395-
answer = "To prevent one process's sampling from shifting another process's results"
398+
answer = (
399+
"To prevent one process's sampling from shifting another process's results"
400+
)
396401
)))
397402
```
398403

@@ -441,7 +446,9 @@ cat(longmcq(c(
441446
```{r, results="asis", echo = FALSE}
442447
cat(longmcq(c(
443448
"It cannot set seeds",
444-
answer = "It relies on a single global stream, making independent streams difficult",
449+
answer = (
450+
"It relies on a single global stream, making independent streams difficult"
451+
),
445452
"It doesn't support exponential sampling"
446453
)))
447454
```

pages/model/patients.qmd

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ class Model:
190190
ss = np.random.SeedSequence(self.run_number)
191191
seeds = ss.spawn(1)
192192
193-
# Create an exponential distribution for patient inter-arrival times
193+
# Initialise distributions
194194
self.arrival_dist = Exponential(mean=self.param.interarrival_time,
195195
random_seed=seeds[0])
196196
@@ -263,7 +263,7 @@ A random seed sequence is created based on the `run_number`.
263263
<br>
264264

265265
```{.python}
266-
# Create an exponential distribution for patient inter-arrival times
266+
# Initialise distributions
267267
self.arrival_dist = Exponential(mean=self.param.interarrival_time,
268268
random_seed=seeds[0])
269269
```
@@ -376,7 +376,7 @@ We'll then create a function for the model (explained line by line below).
376376
#'
377377
#' @param param List. Model parameters.
378378
#' @param run_number Int. Run number for random seed generation.
379-
#'
379+
#'
380380
#' @importFrom simmer add_generator run simmer timeout trajectory
381381
382382
model <- function(param, run_number) {
@@ -389,7 +389,7 @@ model <- function(param, run_number) {
389389
390390
# Define the patient trajectory
391391
patient <- trajectory("patient_path") |>
392-
timeout(1)
392+
timeout(1L)
393393
394394
env <- env |>
395395
# Add patient generator
@@ -474,8 +474,8 @@ The simulation is then run for a specified duration using the `run()` method. Be
474474
To try running the model, set up the parameters and run `model()`.
475475

476476
```{r}
477-
param = create_params()
478-
model(param = param, run_number = 1)
477+
param <- create_params()
478+
model(param = param, run_number = 1L)
479479
```
480480

481481
:::
@@ -845,7 +845,7 @@ create_params <- function(interarrival_time = 5L,
845845
#'
846846
#' @param param List. Model parameters.
847847
#' @param run_number Int. Run number for random seed generation.
848-
#'
848+
#'
849849
#' @importFrom simmer add_generator run simmer timeout trajectory
850850
851851
model <- function(param, run_number) {
@@ -864,7 +864,7 @@ model <- function(param, run_number) {
864864
865865
# Define the patient trajectory
866866
patient <- trajectory("patient_path") |>
867-
timeout(1)
867+
timeout(1L)
868868
869869
env <- env |>
870870
# Add patient generator
@@ -882,8 +882,8 @@ model <- function(param, run_number) {
882882
We can see that results are consistent with before.
883883

884884
```{r}
885-
param = create_params()
886-
model(param = param, run_number = 1)
885+
param <- create_params()
886+
model(param = param, run_number = 1L)
887887
```
888888

889889
:::

pages/output_analysis/n_reps.qmd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
title: ❌ Number of replications
33
---
44

5+
<!--separate seeds for each replication-->
6+
57
<br><br>

pages/setup/code_structure.qmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ Classes are less common in R than functions, but they are used sometimes for com
136136
The example below uses **R6**, since this style is closest to how Python handles classes, making it easier to draw parallels between the two languages.
137137

138138
```{r}
139-
library(R6) # nolint: undesirable_function_linter
139+
library(R6)
140140
141141
Patient <- R6Class("Patient", # nolint: object_name_linter
142142
public = list(

0 commit comments

Comments
 (0)