Description Details Public fields Methods Examples
R6 class representing the world in which the moths live
Just like moths, the world has a colour either 0 (white) or 1 (black). During a simulation, the colour of the world will change with a frequency defined by the attribute 'period'.
time
Integer. Number of time steps that have passed in the world.
colour
Integer. Colour of the world. Can be either 0 (white) or 1 (black).
period
Integer. Number of time steps after which colour of world changes.
new()
Initialize new R6 object of class 'world'.
world$new(period = 100)
period
Integer. Number of time steps after which colour of world changes.
An R6 object of class 'world'
computecolour()
Change colour of the world.
world$computecolour()
World will change colour at a given frequency specified by the attribute 'period'.
#Create a world that should change colour every 50 timesteps set.seed(123L) my_world <- world$new(period = 50) #Colour of world starts as black (i.e. 1) my_world$colour #Once time reaches 50, colour will change to white (i.e. 0) my_world$time <- 50 my_world$computecolour() my_world$colour
moveforward()
Advance time by 1.
world$moveforward()
Time of the world will move forward by 1, after which we check to see if the colour of the world should change given the attribute 'period'.
my_world <- world$new(period = 20) #Starts with time 1 my_world$time #Moves to time 2 after moveforward my_world$moveforward() my_world$time
clone()
The objects of this class are cloneable with this method.
world$clone(deep = FALSE)
deep
Whether to make a deep clone.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | my_world <- world$new(period = 50)
plot(NULL, ylim = c(0, 1), xlim = c(1, 1000), xlab = "time", ylab = "", axes = FALSE)
axis(1)
#Move the world forward 1000 time steps
#World will change colour every 50
for (t in 1:1000) {
my_world$moveforward()
abline(v = t, col = my_world$colour, lwd = 5)
}
## ------------------------------------------------
## Method `world$computecolour`
## ------------------------------------------------
#Create a world that should change colour every 50 timesteps
set.seed(123L)
my_world <- world$new(period = 50)
#Colour of world starts as black (i.e. 1)
my_world$colour
#Once time reaches 50, colour will change to white (i.e. 0)
my_world$time <- 50
my_world$computecolour()
my_world$colour
## ------------------------------------------------
## Method `world$moveforward`
## ------------------------------------------------
my_world <- world$new(period = 20)
#Starts with time 1
my_world$time
#Moves to time 2 after moveforward
my_world$moveforward()
my_world$time
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.