title: I do not understand error "object not found" inside the function
body: <p>I have roughly this function:</p>
  
  <pre><code>plot_pca_models &lt;- function(models, id) {
    library(lattice)
  
    splom(models, groups=id)
  }
  </code></pre>
  
  <p>and I'm calling it like this:</p>
  
  <pre><code>plot_pca_models(data.pca, log$id)
  </code></pre>
  
  <p>wich results in this error:</p>
  
  <pre><code>Error in eval(expr, envir, enclos) : object 'id' not found
  </code></pre>
  
  <p>when I call it without the wrapping function:</p>
  
  <pre><code>splom(data.pca, groups=log$id)
  </code></pre>
  
  <p>it raises this error:</p>
  
  <pre><code>Error in log$id : object of type 'special' is not subsettable
  </code></pre>
  
  <p>but when I do this:</p>
  
  <pre><code> id &lt;- log$id
   splom(models, groups=id)
  </code></pre>
  
  <p>it behaves as expected.</p>
  
  <p>Please can anybody explain why it behaves like this and how to correct it? Thanks.</p>
  
  <p>btw:
  I'm aware of similar questions here, eg:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/6498177/help-understand-the-error-in-a-function-i-defined-in-r">Help understand the error in a function I defined in R</a></li>
  <li><a href="https://stackoverflow.com/questions/6955128/object-not-found-error-with-ddply-inside-a-function">Object not found error with ddply inside a function</a></li>
  <li><a href="https://stackoverflow.com/questions/16048390/object-disappears-from-namespace-in-function">Object disappears from namespace in function</a></li>
  </ul>
  
  <p>but none of them helped me.</p>
  
  <p><strong>edit</strong>:
  As requested, there is full "plot_pca_models" function:</p>
  
  <pre><code>plot_pca_models &lt;- function(data, id, sel=c(1:4), comp=1) {
    # 'data' ... princomp objects
    # 'id'   ... list of samples id (classes)
    # 'sel'  ... list of models to compare
    # 'comp' ... which pca component to compare
  
    library(lattice)
  
    models &lt;- c()
    models.size &lt;- 1:length(data)
    for(model in models.size) {
      models &lt;- c(models, list(data[[model]]$scores[,comp]))
    }
    names(models) &lt;- 1:length(data)
  
    models &lt;- do.call(cbind, models[sel])
  
    splom(models, groups=id)
  }
  </code></pre>
  
  <p><strong>edit2</strong>:
  I've managed to make the problem reproducible.</p>
  
  <pre><code>require(lattice)
  my.data &lt;- data.frame(pca1 = rnorm(100), pca2 = rnorm(100), pca3 = rnorm(100))
  my.id &lt;- data.frame(id = sample(letters[1:4], 100, replace = TRUE))
  
  plot_pca_models2 &lt;- function(x, ajdi) {
    splom(x, group = ajdi)
  }
  
  plot_pca_models2(x = my.data, ajdi = my.id$id)
  </code></pre>
  
  <p>which produce the same error like above.</p>