Copyright © 2007-2024 The OpenMx Project
mxEval looks in the parent environments to find objects. This can be handy, allowing reference to such objects in algebra. However, when used in the context of getting things out of models (quite a common use-case), it can be dangerous: mxEval might return an object with the same name from the environment, rather than from inside the model you have set as the target.
Example. Say you have a matrix
m1$submodels$top$matrices$nVar$values [,1] [1,] 1
If you also have a variable nVar in my environment, set to whatever it happens to be set to, say "2", then mxEval() doesn't return the nVar matrix from the model, but rather nVar from the environment:
nVar = 2 mxEval(nVar, m1)
(parenthetically, I had thought it searched exhaustively inside the model before traversing the parent environment, but this is not the case.)
[1] 2 m1$submodels$top$matrices$nVar$values [,1] [1,] 1
i.e., it has discovered nVar in the environment, even though it exists in the submodel, and there’s no warning.
Did that used to happen?
I suggest we add an option to mxEval(), something like “env = 'self’ ” for when we’d rather get an “object not found” error than risk mistakenly accessing a variable from other environments. It's really pretty hairy...
then this would occur:
mxEval(nVar, m1, env=’self’) # error: var not found mxEval(nVar, m1$top, env=’self’) [,1] [1,] 1 mxEval(nVar, m1, env=’global’) 2