Learn R Programming

SpaDES.core (version 2.0.5)

saveFiles: Save objects using .saveObjects in params slot of simInit

Description

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.

Usage

saveFiles(sim)

Value

(invisibly) the modified sim object. invoked for side effect of saving the simulation to file.

Arguments

sim

A simList simulation object.

Model-level saving

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.

Module-level saving

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.

Custom saving

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.

Author

Eliot McIntire and Alex Chubaty

Details

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.

Examples

Run this code
# \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")
  parameters <- list(
    .globals = list(stackName = "landscape"),
    caribouMovement = list(
      .saveObjects = "caribou",
      .saveInitialTime = 1, .saveInterval = 1,
      .plots = NA
    ),
    randomLandscapes = list(.plots = NA, nx = 20, ny = 20))

  modules <- list("randomLandscapes", "caribouMovement")
  paths <- list(
    modulePath = getSampleModules(tempdir()),
    outputPath = outputPath
  )
  opts <- options("spades.moduleCodeChecks" = FALSE) # not necessary for example
  mySim <- simInit(times = times, params = parameters, modules = modules,
                   paths = paths)

  # The caribou module has a saveFiles(sim) call, so it will save caribou
  spades(mySim)
  dir(outputPath)

  # remove the files
  file.remove(dir(outputPath, full.names = TRUE))

  ## save multiple outputs
  parameters <- list(
    .globals = list(stackName = "landscape"),
    caribouMovement = list(
      .saveObjects = c("caribou", "habitatQuality"),
      .saveInitialTime = 1, .saveInterval = 1,
      .plots = NA
    ),
    randomLandscapes = list(.plots = NA, nx = 20, ny = 20))

  mySim <- simInit(times = times, params = parameters, modules = modules,
                 paths = paths)

  spades(mySim)
  dir(outputPath)
  # remove the files
  file.remove(dir(outputPath, full.names = TRUE))

  options(opts) # clean up

}# }

Run the code above in your browser using DataLab