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

Getting started with SpaDES

Anatomy of a SpaDES module

Directory structure

module.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

Anatomy of a SpaDES module

Directory structure

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(...)

Anatomy of a SpaDES module

Code file structure

samplePath <- system.file('sampleModules', package = 'SpaDES')
openModules('randomLandscapes', samplePath)

A module code file (.R) consists of the following key sections:

  1. module metatadata (defineModule block)

  2. definitions of each module event type (doEvent.moduleName block)

  3. additional functions used in the events above

  4. (optional) block of code that is run during module initialization, used to perform additional data processing/transformation steps (.inputObjects block)

New module template

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:

1. Module metadata

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

1. Module metadata

Module parameters

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!

1. Module metadata

Data dependencies

The inputObjects and outputObjects metadata fields specify a module's inputs and outputs respectively.

These refer to R objects, rather than raw data files.

1. Module metadata

Working with data

2. Module events

3. Additional module functions

4. The .inputObjects block (optional)

4. The .inputObjects block (optional)

Visualizations

SpaDES 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()

Plotting

See 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

Event-level plotting

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")

Interacting with plots

See http://spades.predictiveecology.org/vignettes/iii-plotting.html#interacting-with-plots

Saving

E.g., schedule a recurring save event within a module:

nextSave <- time(mySim) + SpaDES::p(mySim)$.saveInterval
sim <- scheduleEvent(mySim, nextSave, "moduleName", "save")

Loading

Checkpointing

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).

Debugging

See debugging info at the wiki.

Finding SpaDES tools

Categorized overview of the SpaDES package

?SpaDES
  1. Spatial discrete event simulation (SpaDES)
  2. Module functions
  3. Plotting
  4. File operations
  5. Sample data and modules included in package

Summary statistics

See this wiki entry.

  1. Add a new module parameter and output to the module metadata.

  2. Add a 'summarize' event.

  3. Add an new event function that calculates the statistic(s) of interest.

  4. Update your module's reqdPkgs metadata field if you are using additional packages.

Module development checklist

*Adapted from the one on the wiki.

Metadata

Module development checklist

*Adapted from the one on the wiki.

Events

Module development checklist

*Adapted from the one on the wiki.

Documentation

Module development checklist

*Adapted from the one on the wiki.

Data

Module development checklist

*Adapted from the one on the wiki.

Distributing your module



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