R function that parses output printed to console and returns meaningful object
<p>It sometimes occurs in R that a function will print output to the console that is never returned as an object. For example, <code>factanal</code> prints the proportion of variance explained as well as a loading matrix with hidden loadings, but none of these things are directly available to the user as returned objects. While in some cases, there are special extractor functions, often you have to go onto a search into the relevant print method and try to extract the relevant code that generates the information, if you want to use it in subsequent analyses. </p>

<p>Thus, I was curious whether there was a general function that is able to parse printed output in R. Ideally such a function would recognise when a matrix/data.frame is being printed and return that as one element of a list in a data.frame or matrix format.</p>

<p>To make this specific, I use the <code>factanal</code> function. If we run the following code:</p>

<pre><code>fit &lt;- factanal(matrix(rnorm(1000), ncol = 5), 2)
fit
</code></pre>

<p>we get the following output:</p>

<pre><code>Call:
factanal(x = matrix(rnorm(1000), ncol = 5), factors = 2)

Uniquenesses:
[1] 0.005 0.990 0.994 0.807 0.915

Loadings:
     Factor1 Factor2
[1,]  0.974   0.216 
[2,]                
[3,]                
[4,]          0.429 
[5,]          0.289 

               Factor1 Factor2
SS loadings      0.963   0.327
Proportion Var   0.193   0.065
Cumulative Var   0.193   0.258

Test of the hypothesis that 2 factors are sufficient.
The chi square statistic is 0.27 on 1 degree of freedom.
The p-value is 0.601 
</code></pre>

<p>There would be various ways of parsing the above text and returning it as a meaningful object. One option would simply be to return character vector of one line per row. A more sophisticated approach would return a list where each element is split where there is an empty line, and it would somehow recognise where a matrix is being presented. It's quite clear that "Loadings:..." and where it says "Factor1, Factor2" is the beginning of a matrix. </p>

<h3>Question</h3>

<ul>
<li>Is there a general purpose function in R that can be used to extract the elements of printed output?</li>
<li>Alternatively, is there a general approach that works well in extracting output from output printed to the console?</li>
</ul>

<p>For concreteness, I would be interested in how such approaches could be applied to the specific case above applied to extracting the content from a factor analysis.</p>