SpaDES
module - seed establishmentvignettes/Convert-R-code-to-SpaDES-module.Rmd
Convert-R-code-to-SpaDES-module.Rmd
We can imagine a set of sources (e.g., trees with seeds) and a map of “quality” which will affect whether the things (e.g., seeds) will land and establish.
This code does a few things:
# Load some libraries
library(raster)
library(SpaDES)
set.seed(321) # so we can compare amongst ourselves
# Parameters
Nsource <- 20
establishThresh <- 0.1
# Initial Map of Quality -- make a dummy one
quality <- raster(extent(0, 100, 0, 100), res = c(1, 1)) %>%
gaussMap(speedup = 1) # SpaDES function to generate a random map
quality <- scale(quality, center = maxValue(quality), # rescales from 0 to 1
scale = minValue(quality) - maxValue(quality))
# Initial Map of sources (e.g., seed trees maybe)
sourceLocations = initiateAgents(quality, Nsource) # SpaDES function
# Create a distance from source map
distFromSource <- distanceFromPoints(quality, sourceLocations) # raster function
# Create establishment map, a function of inverse distance and quality,
# Actual establishment is determined with a threshold parameter
establish <- (1/distFromSource * quality) > establishThresh
clearPlot()
Plot(distFromSource, title = "Distance from source")
Plot(establish, title = "Establishment successful")
Plot(sourceLocations, addTo = "establish", title = "Establishment successful")
newModule()
to create a blank template..R
file that shows up..inputObjects
section to create the needed inputs.distFromSource
and establish
in the “init” event.plot
event.So far, this only calculates the “establish” map once. What if we had another module that updates “sourceLocation” every year. So, we have to run distanceFromSource
and establish
every year. There are 2 options for this: add it all in this module, or make a new module that would generate the sourceLocations.
sourceLocation
, make new distFromSource
, and make establish
into an event called “annualEstablishment”. You can delete it from the “init” event, or keep it thereAdd 3 scheduleEvent
calls:
within the “annualEstablishment” block, scheduling the “annualEstablishment” event in this (“establishment”) module. We can schedule it for “now” + 1, which would be time(sim) + 1
. This creates the time sequence.
This is “better” because it emulates how the normal situation would be, but it takes a few more steps. But, as we get more agile with modules, this step would take 3 minutes. Basically all the steps are the same as above, but in a different module, rather than within this module.
This solution follows Option 1.