Used to specify a parameter's name, value, and set a default. The min
and
max
arguments are ignored by simInit
or spades
; they
are for human use only. To ensure that a user cannot set parameters outside of
a range of values, the module developer should use assertions in their module
code.
defineParameter(name, class, default, min, max, desc, ...)
Character string giving the parameter name.
Character string giving the parameter class.
The default value to use when none is specified by the user. Non-standard evaluation is used for the expression.
With max
, used to define a suitable range of values.
Non-standard evaluation is used for the expression.
These are not tested by simInit
or
spades
. These are primarily for human use, i.e., to
tell a module user what values the module expects.
With min
, used to define a suitable range of values.
Non-standard evaluation is used for the expression.
These are not tested by simInit
or
spades
. These are primarily for human use, i.e., to
tell a module user what values the module expects.
Text string providing a brief description of the parameter.
If there are extra spaces or carriage returns, these will be stripped,
allowing for multi-line character strings without using paste
or multiple quotes.
A convenience that allows writing a long desc
without
having to use paste
; any character strings after desc
will be paste
d together with desc
.
a data.frame
Be sure to use the correct NA type: logical (NA
), integer (NA_integer_
),
real (NA_real_
), complex (NA_complex_
), or character (NA_character_
).
See NA()
.
parameters = rbind(
defineParameter("lambda", "numeric", 1.23, desc = "intrinsic rate of increase"),
defineParameter("P", "numeric", 0.2, 0, 1, "probability of attack"),
# multi-line desc without quotes on each line -- spaces and carriage returns are stripped
defineParameter("rate", "numeric", 0.2, 0, 1,
"rate of arrival. This is in individuals
per day. This can be modified
by the user"),
# multi-line desc with quotes on each line
defineParameter("times", "numeric", 0.2, 0, 1,
desc = "The times during the year ",
"that events will occur ",
"with possibility of random arrival times")
)
# \donttest{
# Create a new module, then access parameters using `P`
tmpdir <- file.path(tempdir(), "test")
checkPath(tmpdir, create = TRUE)
#> [1] "/tmp/Rtmp45trFG/test"
# creates a new, "empty" module -- it has defaults for everything that is required
newModule("testModule", tmpdir, open = FALSE)
#> New module testModule created at /tmp/Rtmp45trFG/test
#> * edit module code in testModule.R
#> * write tests for your module code in tests/
#> * describe and document your module in testModule.Rmd
#> * tell others how to cite your module by editing citation.bib
#> * choose a license for your module; see LICENSE.md
#> Main module file is: /tmp/Rtmp45trFG/test/testModule/testModule.R
#> Main documentation file is: /tmp/Rtmp45trFG/test/testModule/testModule.Rmd
# Look at new module code -- see defineParameter
if (interactive()) file.edit(file.path(tmpdir, "testModule", "testModule.R"))
# initialize the simList
if (requireNamespace("ggplot2", quietly = TRUE)) {
# Some things not necessary in this example, if not interactive (like plotting)
opts <- if (interactive()) list() else
options(spades.plot = NA, spades.useRequire = FALSE,
spades.moduleCodeChecks = FALSE)
mySim <- simInit(modules = "testModule",
paths = list(modulePath = tmpdir))
# Access one of the parameters -- because this line is not inside a module
# function, we must specify the module name. If used within a module,
# we can omit the module name
P(mySim, module = "testModule") # gets all params in a module
P(mySim, ".useCache", "testModule") # just one param
options(opts)
}
#> Setting:
#> options(
#> spades.modulePath = '/tmp/Rtmp45trFG/test'
#> )
#> Paths set to:
#> options(
#> rasterTmpDir = '/tmp/Rtmp45trFG/SpaDES/scratch/raster'
#> reproducible.cachePath = '/tmp/Rtmp45trFG/reproducible/cache'
#> spades.inputPath = '/tmp/Rtmp45trFG/SpaDES/inputs'
#> spades.outputPath = '/tmp/Rtmp45trFG/SpaDES/outputs'
#> spades.modulePath = '/tmp/Rtmp45trFG/test'
#> spades.scratchPath = '/tmp/Rtmp45trFG/SpaDES/scratch'
#> )
#> terra::terraOptions(tempdir = '/tmp/Rtmp45trFG/SpaDES/scratch/terra'
#> Loading required package: ggplot2
#> Nov21 04:22:27 simInit Using setDTthreads(1). To change: 'options(spades.DTthreads = X)'.
#> Nov21 04:22:27 tstMdl:.inputObjects Running .inputObjects for testModule
#> Nov21 04:22:27 tstMdl:.inputObjects testModule: using dataPath '/tmp/Rtmp45trFG/test/testModule/data'.
#> Elpsed time for simInit: 0.08203888 secs
unlink(tmpdir, recursive = TRUE)
# }