simInit fail to find objects in the global env if their names conflict with objects defined elsewhere
Closed this issue · 4 comments
Here is a minimal reproducible example.
library(SpaDES)
data <- data.frame(1)
sim <- simInit(objects = "data")
class(sim$data)
[1] "function"
environment(sim$data)
[1] <environment: namespace:utils>
Obviously, I forgot to mention that I am using the latest development version of SpaDES.
That is a fairly common problem with R: environments are complicated. This will, therefore, always be a potential problem. The reliable thing to do as a user would be to pass it in as a named list, as suggested in the help for simInit. Since this is an R issue (i.e., name conflicts), I am closing this as not a bug. I will add something to the help file about this, however. Though it seems like a fairly obvious challenge: passing in the object with just its name, when the name might be commonly used is unreliable.
library(SpaDES)
data <- data.frame(1)
sim <- simInit(objects = list(data = data))
class(sim$data)
# [1] "data.frame"
sim$data
# X1
# 1 1
I don't know which mechanisms you are using to fetch the data from the global environment but get(envir = globalenv()) should ensure he is looking in the right place. I will try the named list.
The more detailed answer is that if the code is wrapped inside another block of code, like when doing batch spades runs, it is actually the calling environment that is expected, NOT always the global environment. You are correct about what you said, but that is not the behaviour that is universally desired. It does point out that the help was not quite correct. I will fix that in the help. Thank you.