title: How to save summary(lm) to a file?
body: <p>I'm using R for a pharmacodynamic analysis and I'm fairly new to programming. </p>
  
  <p>The thing is, I'm carrying out linear regression analysis and in the future I will perform more advanced methods. Because I'm performing a large number of analysis (and I'm too lazy to manually copy paste every time I run the script), I would like to save the summaries of the analysis to a file. I've tried different methods, but nothing seems to work. </p>
  
  <p>What I'm looking for is the following as (preferably) a text file:</p>
  
  <pre><code>X_Y &lt;- lm(X ~ Y)
  sum1 &lt;- summary(X_Y)
  
  &gt; sum1
  
  Call:
  lm(formula = AUC_cumulative ~ LVEF)
  
  Residuals:
      Min      1Q  Median      3Q     Max 
  -910.59 -434.11  -89.17  349.39 2836.81 
  
  Coefficients:
               Estimate Std. Error t value Pr(&gt;|t|)    
  (Intercept) 1496.4215   396.5186   3.774 0.000268 ***
  LVEF           0.8243     7.3265   0.113 0.910640    
  ---
  Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
  
  Residual standard error: 619.9 on 104 degrees of freedom
    (32 observations deleted due to missingness)
  Multiple R-squared:  0.0001217, Adjusted R-squared:  -0.009493 
  F-statistic: 0.01266 on 1 and 104 DF,  p-value: 0.9106
  </code></pre>
  
  <p>I've searched for methods to save summary functions to a .csv or .txt, but those files don't represent the data in a way I can understand it. </p>
  
  <p>Things I've tried:</p>
  
  <pre><code>fileConn &lt;- file("output.txt")
  writeLines(sum1, fileConn)
  close(fileConn)
  </code></pre>
  
  <p>This returns: </p>
  
  <pre><code>Error in writeLines(sum1, fileConn) : invalid 'text' argument
  </code></pre>
  
  <p>An attempt using the write.table command gave: </p>
  
  <pre><code>&gt; write.table(Sum1, 'output.csv', sep=",", row.names=FALSE, col.names=TRUE, quote=FALSE)
  Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : cannot coerce class ""summary.lm"" to a data.frame
  </code></pre>
  
  <p>Using the write command:</p>
  
  <pre><code>&gt; write(sum1, 'output.txt')
  Error in cat(list(...), file, sep, fill, labels, append) : argument 1 (type 'list') cannot be handled by 'cat'
  </code></pre>
  
  <p>Then I was getting closer with the following:</p>
  
  <pre><code>&gt; write.table(sum1, 'output.csv', sep=",", row.names=FALSE, col.names=TRUE, quote=FALSE)
  </code></pre>
  
  <p>But this file did not have the same readable information as the printed summary</p>
  
  <p>I hope someone can help, because this is way to advanced programming for me. </p>