Pauline - 1 year ago 158

R Question

I'm using R for a pharmacodynamic analysis and I'm fairly new to programming.

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.

What I'm looking for is the following as (preferably) a text file:

`X_Y <- lm(X ~ Y)`

sum1 <- summary(X_Y)

> 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(>|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

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.

Things I've tried:

`fileConn <- file("output.txt")`

writeLines(sum1, fileConn)

close(fileConn)

This returns:

`Error in writeLines(sum1, fileConn) : invalid 'text' argument`

An attempt using the write.table command gave:

`> 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

Using the write command:

`> write(sum1, 'output.txt')`

Error in cat(list(...), file, sep, fill, labels, append) : argument 1 (type 'list') cannot be handled by 'cat'

Then I was getting closer with the following:

`> write.table(sum1, 'output.csv', sep=",", row.names=FALSE, col.names=TRUE, quote=FALSE)`

But this file did not have the same readable information as the printed summary

I hope someone can help, because this is way to advanced programming for me.

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

I think one option could be `sink()`

which will output the results to a text file rather than the console. In the absence of your dataset I've used `cars`

for an example:

```
sink("lm.txt")
summary(lm(cars$speed ~ cars$dist))
sink() # returns output to the console
```

`lm.txt`

now looks like this:

```
Call:
lm(formula = cars$speed ~ cars$dist)
Residuals:
Min 1Q Median 3Q Max
-7.5293 -2.1550 0.3615 2.4377 6.4179
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 8.28391 0.87438 9.474 1.44e-12 ***
cars$dist 0.16557 0.01749 9.464 1.49e-12 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 3.156 on 48 degrees of freedom
Multiple R-squared: 0.6511, Adjusted R-squared: 0.6438
F-statistic: 89.57 on 1 and 48 DF, p-value: 1.49e-12
```

@Roland 's suggestion of `knitr`

is a bit more involved, but could be worth it because you can knit input, text output, and figures in to one report or html file easily.

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**