knitr::opts_chunk$set(echo = TRUE, eval = FALSE)

Loops: what are they and why use them?

Loop example in R

age <- 1
for (time in 1:10) {
  age <- age + 1
}

From loops to events

From loops to events

In SpaDES, events are first defined, then scheduled to happen at a particular point in time:

Events in SpaDES

## initialisation
age <- 1                              

## boundaries
times = list(start = 1, end = 10) 

## event definition (content)
aging <- function(age) {
  age <- age + 1                
}

## event execution and scheduling
events <- {
  doEvent("aging")
  scheduleEvent("aging", when = now + 1)
}

Events in SpaDES

What would that sequence look like?

  1. what is happening now
  2. What are the next thing(s) in the queue
#     eventTime moduleName  eventType
#  1:         0       loop       init
#  2:         0       loop addOneYear
#  3:         1       loop addOneYear
#  4:         2       loop addOneYear
#  5:         3       loop addOneYear
#  6:         4       loop addOneYear
#  7:         5       loop addOneYear
#  8:         6       loop addOneYear
#  9:         7       loop addOneYear
# 10:         8       loop addOneYear
# 11:         9       loop addOneYear
# 12:        10       loop addOneYear 

Why contain the iterated code in a function?



PredictiveEcology/SpaDES.Workshops documentation built on Jan. 30, 2021, 6:52 p.m.