Load times for objects passed to simInit

12 views
Skip to first unread message

Jean Marchal

unread,
Dec 23, 2017, 10:28:25 AM12/23/17
to SpaDES Users
Hi list,

I wonder if there is a way to load objects at given times by using the objects field of simInit () ? That is, something that would work in the same way as for inputs by setting the loadTime parameter.

Thanks,

Jean

Eliot McIntire

unread,
Dec 24, 2017, 3:57:30 PM12/24/17
to SpaDES Users
That capability is in the inputs argument. Just don't give a filename and it will take the object from the global environment at the loadTime values indicated.

Jean Marchal

unread,
Dec 28, 2017, 9:29:59 PM12/28/17
to SpaDES Users

I tried the following:

a <- 1
inputs <- data.frame(objectName = "a", loadTime = 1)

Got the error "Error in dynGet(obNam) : ‘a’ not found"

If I supply the file argument it will search on disk so this does not work either.

How should I do this ?

Thanks,

Jean

Eliot McIntire

unread,
Dec 28, 2017, 9:45:54 PM12/28/17
to spades...@googlegroups.com
There was a bad merge in recent changes. Try the latest development branch (as of 3 minutes ago). Also, note, you will need the latest version of quickPlot (i.e., within the last few days). The install of the latest SpaDES.core will tell you if the version number of quickPlot is too old.

Jean Marchal

unread,
Dec 28, 2017, 10:02:20 PM12/28/17
to SpaDES Users
The following now works!


a <- 1
inputs <- data.frame(objectName = "a", loadTime = 1)

However, this does not do what I want because it disables the ability to rename the input through objectName. Let's say a module expects input "a" and that input "a" should be overwritten by new inputs at different loadTime (or sim times), it seems to me that the only way to do this is to read different files from disk by specifying different loadTimes.

Jean Marchal

unread,
Dec 28, 2017, 10:07:14 PM12/28/17
to SpaDES Users
Just to translate what I just wrote in code:

This is doable:

inputs <- rbind(
  data.frame(objectName = "a", file = "a1.rds", loadTime = 1),
  data.frame(objectName = "a", file = "a2.rds", loadTime = 2)
)

but not the following because the module will expect object "a" not "a1" or "a2":

inputs <- rbind(
  data.frame(objectName = "a1", loadTime = 1),
  data.frame(objectName = "a2", loadTime = 2)
)

Eliot McIntire

unread,
Dec 28, 2017, 10:17:02 PM12/28/17
to SpaDES Users
You are correct. A work around is to create a manual object renaming in an event. Note that the loading via this mechanism puts it at priority 1, so it happens "first" at its given load time. The renaming event can index by time or whatever, so it finds the correct object in the global environment. 

Perhaps a true solution is to add another optional column: sourceObjectName or something or rename the "file" column to be "source", but then it breaks backwards compatibility and also we can't distinguish loading from an R environment and a relative path filename which would not have "/" indicating it unambiguously as a file path (though see reproducible::asPath). It shouldn't be anything specific to Global Env because the user may be running in a non-interactive session. Would that work? If so, please create an issue on github.com/PredictiveEcology/SpaDES.core/issues
Message has been deleted

Eliot McIntire

unread,
Dec 29, 2017, 2:59:43 AM12/29/17
to spades...@googlegroups.com
Actually, there is enough flexibility in the function to do what you need, but you need to use the "arguments" columns. However, there were a few bugs encountered while working on this. I have fixed bugs (development version on github.com >= 0.1.0.9005) , and added an example in ?inputs that does this, i.e., load objects from the global environment, where the objects in the global env are different at each load time, but they overwrite the same object in the simList. But to copy that example here:


# two global objects
  a1 <- 1
  a2 <- 2

  # Note 'arguments' must be a list of NROW(inputs), with each element itself being a list,
  #  which is passed to do.call(fun[x], arguments[[x]]), where x is row number, one at a time
  args <- lapply(1:2, function(x) {
                 list(x = paste0("a", x),
                      envir = environment()) # may be necessary to specify in which envir a1, a2
                                             # are located, if not in an interactive sessino
                 })
  inputs <- data.frame(objectName = "a", loadTime = 1:2, fun = "get", arguments = I(args))
  a <- simInit(inputs = inputs, times = list(start = 0, end = 1))
  a <- spades(a)
  identical(a1, a$a)

  end(a) <- 3
  a <- spades(a) # different object (a2) loaded onto a$a
  identical(a2, a$a)



Jean Marchal

unread,
Dec 29, 2017, 9:52:16 AM12/29/17
to SpaDES Users
This is perfect! Thanks!

Jean
Reply all
Reply to author
Forward
0 new messages