knitr::opts_chunk$set(cache = TRUE, echo = TRUE, eval = FALSE)
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:
Creates 2 default maps... could be loading default initial maps from files instead, if we had these.
"Source" locations.
A "quality" map.
Determines "distance" from source map.
# 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.Fill in metadata:
Indicate libraries that are needed
Indicate object(s) that is/are expected as an input, and object(s) that is/are created as output(s)
Fill in .inputObjects
section to create the needed inputs.
distFromSource
and establish
in the "init" event.plot
event..Rmd
file that we will use.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 "init" block, scheduling the "annualEstablishment" event in this ("establishment") module. We can schedule it for "the beginning", which would be start(sim).
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.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.