In the simInit()
call, a parameter called .saveObjects
can be provided in
each module.
This must be a character string vector of all object names to save. These objects will
then be saved whenever a call to saveFiles
is made.
saveFiles(sim)
(invisibly) the modified sim
object.
invoked for side effect of saving the simulation to file.
The file names will be equal to the object name plus time(sim)
is
appended at the end.
The files are saved as .rds
files, meaning, only one object gets
saved per file.
For objects saved using this function, the module developer must create save
events that schedule a call to saveFiles
.
If this function is used outside of a module, it will save all files in the
outputs(sim)
that are scheduled to be saved at the current time in the simList.
There are several ways to save objects using SpaDES
.
It is not possible to schedule separate saving events for each object
that is listed in the .saveObjects
.
Using the outputs
slot in the simInit()
call.
See example in simInit()
.
This can be convenient because it gives overall control of many modules at a
time, and it gets automatically scheduled during the simInit()
call.
Using the saveFiles
function inside a module.
This must be accompanied by a .saveObjects
vector or list element in the
params
slot in the simList()
.
Usually a module developer will create this method for future users of their module.
A module developer can save any object at any time inside their module, using
standard R functions for saving R objects (e.g., save
or saveRDS
).
This is the least modular approach, as it will happen whether a module user
wants it or not.
# \donttest{
if (requireNamespace("SpaDES.tools", quietly = TRUE) &&
requireNamespace("NLMR", quietly = TRUE)) {
## This will save the "caribou" object at the save interval of 1 unit of time
## in the outputPath location
outputPath <- file.path(tempdir(), "test_save")
times <- list(start = 0, end = 1, "month")
modules <- list("randomLandscapes", "caribouMovement")
paths <- list(
modulePath = getSampleModules(tempdir()),
outputPath = outputPath
)
opts <- options("spades.moduleCodeChecks" = FALSE,
"spades.useRequire" = FALSE) # not necessary for example
## save multiple outputs
parameters <- list(
.globals = list(stackName = "landscape"),
caribouMovement = list(
.saveObjects = c("caribou", "habitatQuality"),
.saveInitialTime = 1, .saveInterval = 1
),
randomLandscapes = list(.plots = NA, nx = 20, ny = 20))
mySim <- simInit(times = times, params = parameters, modules = modules,
paths = paths)
spades(mySim, .plotInitialTime = NA) # plotting not relevant for this example
dir(outputPath)
# remove the files
file.remove(dir(outputPath, full.names = TRUE))
options(opts) # clean up
}# }
#> Setting:
#> options(
#> spades.outputPath = '/tmp/RtmpIQ0Aky/test_save'
#> spades.modulePath = '/tmp/RtmpIQ0Aky/sampleModules'
#> )
#> Paths set to:
#> options(
#> rasterTmpDir = '/tmp/RtmpIQ0Aky/SpaDES/scratch/raster'
#> reproducible.cachePath = '/tmp/RtmpIQ0Aky/myProject/cache'
#> spades.inputPath = '/tmp/RtmpIQ0Aky/myProject/inputs'
#> spades.outputPath = '/tmp/RtmpIQ0Aky/test_save'
#> spades.modulePath = '/tmp/RtmpIQ0Aky/sampleModules'
#> spades.scratchPath = '/tmp/RtmpIQ0Aky/SpaDES/scratch'
#> )
#> terra::terraOptions(tempdir = '/tmp/RtmpIQ0Aky/SpaDES/scratch/terra'
#> Jan27 19:04:08 simInit Using setDTthreads(1). To change: 'options(spades.DTthreads = X)'.
#> Jan27 19:04:08 simInit The following .globals were used:
#> Jan27 19:04:08 simInit Key: <global, module>
#> Jan27 19:04:08 simInit module global
#> Jan27 19:04:08 simInit <char> <char>
#> Jan27 19:04:08 simInit 1: caribouMovement stackName
#> Jan27 19:04:08 simInit 2: randomLandscapes stackName
#> Elpsed time for simInit: 0.07877111 secs
#> Jan27 19:04:08 simInit Using setDTthreads(1). To change: 'options(spades.DTthreads = X)'.
#> Jan27 19:04:08 chckpn:init total elpsd: 0.081 secs | 0 checkpoint init 0
#> Jan27 19:04:08 save :init total elpsd: 0.083 secs | 0 save init 0
#> Jan27 19:04:08 prgrss:init total elpsd: 0.085 secs | 0 progress init 0
#> Jan27 19:04:08 load :init total elpsd: 0.087 secs | 0 load init 0
#> Jan27 19:04:08 rndmLn:init total elpsd: 0.089 secs | 0 randomLandscapes init
#> Jan27 19:04:09 rndmLn:init New objects created:
#> Jan27 19:04:09 rndmLn:init <char>
#> Jan27 19:04:09 rndmLn:init 1: landscape
#> Jan27 19:04:09 crbMvm:init total elpsd: 0.5 secs | 0 caribouMovement init 1
#> Jan27 19:04:09 crbMvm:init New objects created:
#> Jan27 19:04:09 crbMvm:init <char>
#> Jan27 19:04:09 crbMvm:init 1: caribou
#> Jan27 19:04:09 crbMvm:move total elpsd: 0.51 secs | 1 caribouMovement move 5
#> Jan27 19:04:09 crbMvm:move New objects created:
#> Jan27 19:04:09 crbMvm:move <char>
#> Jan27 19:04:09 crbMvm:move 1: habitatQuality
#> Jan27 19:04:09 crbMvm:save total elpsd: 0.56 secs | 1 caribouMovement save 11
#> Jan27 19:04:11 crbMvm:save caribouMovement
#> simList saved in
#> SpaDES.core:::savedSimEnv()$.sim
#> It will be deleted at next spades() call.