title: Error: object '->' not found in R
body: <p>I have a model object described as a named list of vector and matrix parameters. Two basic operations with this class of objects are the loading and storing from/to a numeric vector, based on a <strong>mapping</strong> between the elements of each parameter vector or matrix and the indices in the vector. Here is a simplified example:</p>
  
  <pre><code>LoadModelFromVector &lt;- function(vecParams) {
      model &lt;- list(
          A = diag(5),            # a diagonal square matrix
          B = matrix(0, 5, 5)     # un upper triangular square matrix
      ) 
      attr(model, "p") &lt;- 15
  
      diag(model$A) &lt;- vecParams[1:5]
      model$B[upper.tri[model$B]] &lt;- vecParams[5 + (1:(5*(5-1)/2)))]
  
      model
  }
  
  StoreModelToVector &lt;- function(model) {
      vecParams &lt;- double(length = attr(model, "p"))
      vecParams[1:5] &lt;- diag(model$A)
      vecParams[5 + (1:(5*(5-1)/2)))] &lt;- model$B[upper.tri[model$B]]
  
      vecParams
  }
  </code></pre>
  
  <p>I don't like the above example because it replicates the mapping in two places in the code. Instead, I would like to have this mapping in one place. I thought that this could be elegantly done using an abstraction of the assignment operator <code>&lt;-</code>:</p>
  
  <pre><code>LoadStoreModel &lt;- function(vecParams, model = NULL) {
      if(is.null(model)) {
          model &lt;- list(
              A = diag(5),            # a diagonal square matrix
              B = matrix(0, 5, 5)     # un upper triangular square matrix
          )
          `%op%` &lt;- `&lt;-`              # WORKS FINE :-)
          mode &lt;- "load"
      } else {
          vecParams &lt;- double(length = attr(model, "p"))
          `%op%` &lt;- `-&gt;` # GENERATES "Error: object '-&gt;' not found" :-(
          mode &lt;- "store"
      } 
  
      diag(model$A) %op% vecParams[1:5]
      model$B[upper.tri[model$B]] %op% vecParams[5 + (1:(5*(5-1)/2)))]
  
      if(mode == "load") {
          model
      } else {
          vecParams
      }
  }
  
  LoadModelFromVector(vecParams) {LoadStoreModel(vecParams)}
  StoreModelToVector(model) {LoadStoreModel(NULL, model)}
  </code></pre>
  
  <p>The above code generates the error "Error: object '->' not found". Both operators, '->' and '&lt;-', are documented in the R help page <code>?assignOps</code> from the package base.</p>