OK, so I'm trying to use the S4 classes to create a very complex object with slots, including half a dozen matrices, several lists, and probably a kitchen sink or two there. The object is initialized by referencing and unpacking the configuration object that I have already defined. It's easy to define a class using setClass (), but it's hard for me to understand the elegant way to set slots in setMethod ("initialize").
The problem is that I need to set certain elements of these half-dozen matrices based on parts of this configuration object. For each element of the configuration object, I may need to set certain elements from several matrices. Note that the matrices are in the area / environment of the initialization function. Then I have nested functions in the initialization functions that do the actual assignment to matrices or that idea anyway. These functions can, of course, see matrices, but they cannot change them, because the <- operator creates a new matrix if the original variable was not defined in the current environment. R is the transmission by value and means it. This is even true for the .Object slots I'm trying to initialize. Therefore, I cannot use nested functions to initialize.
Unfortunately, these nested functions must modify several matrices, so the return values and job execution in the main initialization function are not practical or elegant. (But it’s possible if I stuffed copies of the matrices into the returned lists, and then combined them into the main initialization function. Awful, although it would require a lot of additional code.)
An iteration (which would prevent this problem) is not very practical due to the hierarchical nature of the configuration object, which really wants to go through recursive calls.
The last option I can think of is to use the assign () function with the envir option to force the assignment to apply to a non-local variable. But using such environments seems bad, like the goto statement ...
, ? , ? , ? , ?