vignettes/Module-details.Rmd
Module-details.Rmd
SpaDES
Our Getting Started Guide is available from the wiki.
The modules vignette contains much more detail.
SpaDES
modulemodule.path <- file.path(dirname(tempdir()), "modules")
downloadModule('wolfAlps', module.path, data = TRUE)
list.files(file.path(module.path, 'wolfAlps'), all.files = TRUE)
/moduleRepository
|_ moduleName/
|_ R/ # contains additional .R (helper) files
|_ data/ # directory for all included data
|_ CHECKSUMS.txt # contains checksums for data files
|_ tests/ # contains unit tests for module code
|_ citation.bib # bibtex citation for the module
|_ LICENSE.txt # describes module's legal usage
|_ README.txt # provide overview of key aspects
|_ moduleName.R # module code file (incl. metadata)
|_ moduleName.Rmd # documentation, usage info, etc.
|_ moduleName_x.y.z.zip # zip archive of previous versions
Advanced: not included (yet) in the main module repo are examples of modules that include unit tests and code coverage.
These are encouraged and can be built using:
# when creating a new module
newModule(..., unitTests = TRUE) # default
# or after-the-fact with
newModuleTests(...)
samplePath <- system.file('sampleModules', package = 'SpaDES')
openModules('randomLandscapes', samplePath)
A module code file (.R
) consists of the following key sections:
module metatadata (defineModule
block)
definitions of each module event type (doEvent.moduleName
block)
additional functions used in the events above
(optional) block of code that is run during module initialization, used to perform additional data processing/transformation steps (.inputObjects
block)
The newModule
function creates a module template for you to edit to suit your needs:
newModule('moduleName', file.path('path/to/my/module/directory'))
Alternatively, use the RStudio addin which is simply a GUI wrapper for this function:
Each module requires a collection of metadata describing the module, its dependencies, and linking to documentation, etc.
These metatadata are defined in the defineModule
code block at the start of the file, and are intended to be both human and machine readable.
## see section 'Required metadata elements'
?defineModule
Parameters defined in the module’s defineParameter
block are module-specific.
This is where default parameter values are specified (which can be overridden by the user during simInit()
).
They can be accessed using params(sim)$module$param
or P(sim)$param
. The latter is context-dependent!
The inputObjects
and outputObjects
metadata fields specify a module’s inputs and outputs respectively.
These refer to R objects, rather than raw data files.
sourceURL
field in module metadata is used for URLs of external data, which are downloaded using downloadData()
Each event consists of two parts:
scheduleEvent()
)To keep this section as easy to read as possible, use additional module functions (defined in the section below).
envir
), rather than pass them as function arguments.envir
is similar to accessing items in a list, i.e., sim[["object"]]
or sim$object
can be used.modulenameEventtype()
..inputObjects
block (optional)How do we provide default data for a module?
Once we have defined what the module is looking for (in the inputObjects
block of defineModule
), we may want to supply a default data set or links to raw data sources online
?defineModule
and ?inputs
for details.Sequence to fill simList with data, each subsequent one will override the previous:
.inputObjects
function in moduleobjects
argument in simInit
inputs
argument in simInit
simInit
, it will override the defaultsSpaDES
builds on R’s exceptional graphics capabilities, and the standard R visualization packages etc. can be used with SpaDES
.
However, for fast prototyping and on-the-fly graphics useful for module debugging and development you should use Plot()
.
- much faster than base graphics, ggplot, etc.
- modular plotting (automatic multipanel layouts)
WARNING: The built-in RStudio plotting device is heinously slow!
if (getOption('device') == 'RStudioGD') dev.useRSGD(FALSE)
dev()
Plot
tingSee the plotting vignette and ?Plot
for more detailed examples.
Plot(...)
clearPlot()
Plot(..., new = TRUE)
Plot(..., addTo = objectName) ## adds to existing plot
rePlot() ## useful after plot device is resized
## see also
colors(...) ## get and set raster colors
Module-specific plot parameters can be used to control plotting for your event: .plotInitialTime
and .plotInterval
.
E.g., schedule a recurring plot event within a module:
nextPlot <- time(mySim) + SpaDES::p(mySim)$.plotInterval
mySim <- scheduleEvent(mySim, nextPlot, "moduleName", "plot")
See http://spades.predictiveecology.org/vignettes/iii-plotting.html#interacting-with-plots
clickValues()
clickExtent()
http://spades.predictiveecology.org/vignettes/ii-modules.html#load-and-.save-modules
.saveObjects
saveFiles()
E.g., schedule a recurring save event within a module:
nextSave <- time(mySim) + SpaDES::p(mySim)$.saveInterval
sim <- scheduleEvent(mySim, nextSave, "moduleName", "save")
Checkpointing is build into SpaDES
automatically and can be turned on at the simulation level (not the module level).
parameters <- list(
.checkpoint = list(interval = 10, file = "chkpnt.RData")
)
mySim <- simInit(..., params = parameters)
See vignette for more details.
NOTE don’t checkpoint too often, or your simulation will slow down too much (disk writes are slow).
using spades(sim, debug = TRUE)
adding browser()
calls to your module code
using the Rstudio debugger
See debugging info at the wiki.
See this wiki entry.
Add a new module parameter and output to the module metadata.
Add a ‘summarize’ event.
Add an new event function that calculates the statistic(s) of interest.
Update your module’s reqdPkgs
metadata field if you are using additional packages.
*Adapted from the one on the wiki.
moduleDiagram
and objectDiagram
to confirm how data objects are passed among modules.*Adapted from the one on the wiki.
sim$function(sim)
to access event functionsuse sim$object
to access simulation data objectssim[[globals(sim)$objectName]]
to access variable-named objectsmoduleNameFunction()
instead of function()
.sim$function
notation is not required for the definition of event functions*Adapted from the one on the wiki.
*Adapted from the one on the wiki.
data/
sourceURL
metadata field.inputObjects
are correctCHECKSUMS.txt
file for all data using checksums(..., write = TRUE)
*Adapted from the one on the wiki.