Object-Oriented Epidemics Simulation

RaukR 2026 • Data Science With R

Object-oriented epidemics simulation using R6 class system
Author

Marcin Kierczak

Published

18-Aug-2026

Note

During this lab, we will be simulating spreading of a disease using R6 OOP system.

1 Define an agent

library('R6')

Agent <- R6Class(classname = "Agent",
  public = list(
    var1 = character()
    initialize = function(var = "") {
      self$var = "initial value"
    },
    some_other_function = function(a, b, c) {

    }
  )
)
Code
library(R6)

Agent <- R6Class("Agent",
  public = list(
    state = character(),
    initialize = function(state = "healthy") {
      self$state <- state
    },
    update_state = function(sick_prob, recovery_prob, death_prob) {
      # If agent is healthy it can either stay healthy or get infected
      if (self$state == "healthy") {
        new_state <- sample(x = c("healthy", "sick"), 
                       size = 1, 
                       prob = c(1-sick_prob, sick_prob))
      # A sick agent can continue being sick, recover and become immune or die
      } else if (self$state == "sick") { 
        new_state <- sample(x = c("sick", "immune", "dead"), 
                       size = 1, 
                       prob = c((1-(recovery_prob + death_prob)), recovery_prob, death_prob))
      
      } else {
        new_state <- self$state # Immune and dead states do not change
      }
      self$state <- new_state  # Update state
    }
  )
)

2 Define the world

Congratulations! You are familiar with S3, S4 and R6 object models by now!

3 Session

Click here
sessionInfo()
R version 4.6.0 (2026-04-24)
Platform: aarch64-apple-darwin23
Running under: macOS Tahoe 26.5.2

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.6/Resources/lib/libRblas.0.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.6/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.1

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: Europe/Stockholm
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] progress_1.2.3   gganimate_1.0.11 lubridate_1.9.5  forcats_1.0.1   
 [5] stringr_1.6.0    dplyr_1.2.1      purrr_1.2.2      readr_2.2.0     
 [9] tidyr_1.3.2      tibble_3.3.1     ggplot2_4.0.3    tidyverse_2.0.0 
[13] R6_2.6.1        

loaded via a namespace (and not attached):
 [1] gtable_0.3.6       jsonlite_2.0.0     crayon_1.5.3       compiler_4.6.0    
 [5] tidyselect_1.2.1   scales_1.4.0       yaml_2.3.12        fastmap_1.2.0     
 [9] generics_0.1.4     knitr_1.51         htmlwidgets_1.6.4  pillar_1.11.1     
[13] RColorBrewer_1.1-3 tzdb_0.5.0         rlang_1.3.0        stringi_1.8.9     
[17] xfun_0.60          S7_0.2.2           otel_0.2.0         timechange_0.4.0  
[21] cli_3.6.6          tweenr_2.0.3       withr_3.0.3        magrittr_2.0.5    
[25] digest_0.6.39      grid_4.6.0         hms_1.1.4          lifecycle_1.0.5   
[29] prettyunits_1.2.0  vctrs_0.7.3        evaluate_1.0.5     glue_1.8.1        
[33] farver_2.1.2       rmarkdown_2.31     tools_4.6.0        pkgconfig_2.0.3   
[37] htmltools_0.5.9