title: How to summarize the elements in a complex object?
body: <p>I have a very complex S4 object (output from a <a href="https://cran.r-project.org/web/packages/lavaan/lavaan.pdf" rel="nofollow noreferrer">lavaan</a> model) which has slots within slots within slots and variables (<code>$</code>) at the deepest level of each of the deepest slots. How do I extract and store the <code>object.size</code>(and potentially other functions like <code>length</code> and <code>dim</code> and the object name) of every element within this object so that I can compare it to another object of the same class?</p>
  
  <p>I have tried storing the output from <code>str(obj)</code> and <code>unclass(obj)</code> and then manipulating the output to extract the information I want, but it's turning out to be very tedious. Looping over names is equally difficult. Is there a way to "flatten" the object into a list? Is there a recursive function anyone can think of to repeatedly dig into each slot?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>Here's an example, using the <code>lavaan</code> package I referenced above, though ideally the solution shouldn't be dependent on the specific object class and could work across classes:</p>
  
  <pre><code>library(lavaan)
  model &lt;- '
    # measurement model
      ind60 =~ x1 + x2 + x3
      dem60 =~ y1 + y2 + y3 + y4
      dem65 =~ y5 + y6 + y7 + y8
    # regressions
      dem60 ~ ind60
      dem65 ~ ind60 + dem60
    # residual correlations
      y1 ~~ y5
      y2 ~~ y4 + y6
      y3 ~~ y7
      y4 ~~ y8
      y6 ~~ y8
  '         
  fit &lt;- sem(model, data=PoliticalDemocracy)
  </code></pre>
  
  <p>The object <code>fit</code> contains many slots and objects inside. I can, of course, extract information from a particular element like <code>object.size(fit@Data@X[[1]])</code>, but I'm looking for a generalized solution. The challenge is that I want to extract the same information about each element, regardless of its "depth".</p>
  
  <p>Thanks!</p>