defineParameter not behaving the way I expect it to

Skip to first unread message

Jul 25, 2019, 1:42:23 PM7/25/19
to SpaDES Users
Hello, I have noticed that the defineParameter function does not behave like I would expect regarding min and max range in its argument

example snippet:
defineModule(sim, list(
parameters= rbind(
 defineParameter("VarName", "numeric", 1, 1, 2, "Variable1"),
 defineParameter("VarName2", "numeric", 10, 1, 2, "Variable2")

I would expect that upon starting the simulation, it would throw a warning or better yet an error since the default value of VarName2 is out of range (=10 when it should be within 1<=x<=2) but I get no such thing.This is true even if I define the parameters within the simInit function

Did I understand the description correctly or am I missing something? 

Thanks for your help


Louis-Etienne Robert

Jul 30, 2019, 11:38:18 AM7/30/19
to SpaDES Users
Somebody replied to me answering my question, however the response is not shown within the public (at least for me) so I am just putting this here for the records in the hope that it can be useful to other people. 

In essence the response is that defineParameter is cosmetic only and they are no check enforced on min/max. You have to build a test within your own module 

If I can make a suggestion, this is not very clear from the documentation and its easy to be misled. Maybe a quick sentence in the function definition could help new users understand this point? (Or better, make the test enforceable from within the function if development time can be assigned to this...)

This is mildly related but I think its important that it is "out-there" for reference purpose
I have been trying to get an object for all the simulation parameters across module because I work with many parameters and I don't want to write a test for every single one of them. Thus I was fiddling with the P(), parameters() params() function. Despite being described as similar, there are some interesting differences among the output of each of these function which could be important for other people 

The slight differences are in the output of P(), parameters() , params() and the way default parameters (.plotInitialTime, .plotInterval, .saveInitialTime, .saveInterval, .useCache) are recorded in these variation of the same function.

So, here is what kind of output you can expect from each variation of the P() family function:


P(sim) return a  list of the module Parameters that is currently being processed by the simulation. Default values are overwritten with user-defined value


params(sim) return a list of list in the form of list of module/ list of parameters.   with .checkpoint and .progress included as "module". Each module entry has a list of the module parameters and their value. Default values are overwritten by users-defined values. Default Parameters are included even if deleted from the metadata


parameters(sim, asDF=FALSE) return a list of list of list in the form of list of module / list of parameter / list of the value of arguments in defineParameter(). Default values are NOT overwritten by the user-defined values (it is the argument found in the defineParameters()). Default Parameters are not included if you deleted them

Thanks for the quick reply and I hope this is useful


Eliot McIntire

Jul 30, 2019, 2:58:39 PM7/30/19
to SpaDES Users

Thanks for this detailed response. It helps us a lot. We have thought a lot about having the min and max values be checked automatically and concluded that it only works for a sub-class of "simple" tests. e.g., what about NAs, are they allowed?  Even for numerics, there is nuance also, if minimum is 0 is that x > 0 or x >= 0, and how would you distinguish these with the simple "min" and "max" arguments.

The main purpose at the moment for these is in the (still very experimental) SpaDES.shiny::shine and other shiny functions.  It sets the slider limits.

We will update the documentation, as you suggest.

Perhaps a possibility we could do is to put the assertion as a quoted string as an argument in defineParameter, e.g., something like:

defineParameter("probSpread", "numeric", 0.5, 0, 1, assertion = "all(probSpread >= 0 & probSpread <= 1 & !")

Again, because of the idiosyncratic nature, it would be hard to provide a non-generic message to a user, as in example below.
This will not be implemented any time soon.

So probably the best solution is, as you have indicated it is somewhat onerous, is to specify this in the module code for now. This allows a specific error message. e.g.,

Init <- function(sim) {
  if (!all(probSpread >= 0 & probSpread <= 1 & !}) stop("probSpread must be between 0 and 1 and have no NAs")

Reply all
Reply to author
0 new messages