R/suppliedElsewhere.R
suppliedElsewhere.Rd
When loading objects into a simList
, especially during the
simInit
call, and inside the .inputObjects
functions of modules,
it is often useful to know if an object in question will or has been
by the user via the inputs
or objects
arguments, or by another
module's .inputObjects
while preparing its expected inputs (via
expectsInputs
in metadata), or if it will be supplied by another
module during its "init"
event. In all these cases, it may not
be necessary for a given module to load any default value for its expectsInputs
.
This function can be used as a check to determine whether the module needs
to proceed in getting and assigning its default value.
suppliedElsewhere(
object,
sim,
where = c("sim", "user", "initEvent"),
returnWhere = FALSE
)
Character vector
A simList
in which to evaluated whether the object is supplied elsewhere
Character vector with one to three of "sim"
, "user"
, or "initEvent"
.
Default is all three. Partial matching is used. See details.
Logical, default FALSE
, whether the vector of length
3 logical should be returned, or a logical of length one
logical
where
indicates which of three places to search, either "sim"
i.e.,
the simList
, which would be equivalent to is.null(sim\$objName)
, or
"user"
which would be supplied by the user in the simInit
function
call via outputs
or inputs
(equivalent to
(!('defaultColor' \%in\% sim$.userSuppliedObjNames))
),
or "initEvent"
, which would test whether a module that gets loaded before
the present one will create it as part of its outputs (i.e., as indicated by
createsOutputs
in that module's metadata). There is a caveat to this test,
however; if that other event also has the object as an expectsInput
, then
it would fail this test, as it also needs it as an input.
This final one ("initEvent"
) does not explicitly test that the object will be created
in the "init" event, only that it is in the outputs of that module, and that it is a module
that is loaded prior to this one.
mySim <- simInit()
#> Setting:
#> options(
#> reproducible.cachePath = '/tmp/Rtmp45trFG/cache'
#> spades.inputPath = '/tmp/Rtmp45trFG/inputs'
#> spades.outputPath = '/tmp/Rtmp45trFG'
#> spades.modulePath = '/tmp/Rtmp45trFG/sampleModules'
#> spades.scratchPath = '/tmp/Rtmp45trFG/scratch'
#> )
#> Nov21 04:22:59 simInit Using setDTthreads(1). To change: 'options(spades.DTthreads = X)'.
#> Elpsed time for simInit: 0.02791619 secs
suppliedElsewhere("test", mySim) # FALSE
#> [1] FALSE
# supplied in the simList
mySim$test <- 1
suppliedElsewhere("test", mySim) # TRUE
#> [1] TRUE
test <- 1
# supplied from user at simInit time -- note, this object would eventually get into the simList
# but the user supplied values come *after* the module's .inputObjects, so
# a basic is.null(sim$test) would return TRUE even though the user supplied test
mySim <- simInit(objects = list("test" = test))
#> Setting:
#> options(
#> reproducible.cachePath = '/tmp/Rtmp45trFG/cache'
#> spades.inputPath = '/tmp/Rtmp45trFG/inputs'
#> spades.outputPath = '/tmp/Rtmp45trFG'
#> spades.modulePath = '/tmp/Rtmp45trFG/sampleModules'
#> spades.scratchPath = '/tmp/Rtmp45trFG/scratch'
#> )
#> Nov21 04:22:59 simInit Using setDTthreads(1). To change: 'options(spades.DTthreads = X)'.
#> Elpsed time for simInit: 0.03113914 secs
suppliedElsewhere("test", mySim) # TRUE
#> [1] TRUE
# \donttest{
# Example with prepInputs
# Put chunks like this in your .inputObjects
if (!suppliedElsewhere("test", mySim))
sim$test <- Cache(prepInputs, "raster.tif", "downloadedArchive.zip",
destinationPath = dataPath(sim), studyArea = sim$studyArea,
rasterToMatch = sim$otherRasterTemplate, overwrite = TRUE)
# }