export R output to .txt file

Hi!

I am somewhat new to R, and I don't know how to export the results of a statistical test that I conducted to a text file. I would like to save these results on my desktop so I don't have to go back and run the tests all over again to see the results! This is what my console looks like:

`> summary(AtTotal_Model)

Call:
lm(formula = Total_amino_acids ~ Genotype * Timepoint, data = AtAAdata)

Residuals:
Min 1Q Median 3Q Max
-2.7262 -1.3018 -0.1063 0.6585 3.5841

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 18.2507 1.1737 15.549 2.57e-09 ***
GenotypeWT -0.7114 1.6599 -0.429 0.6758
TimepointEN 0.4763 1.6599 0.287 0.7790
TimepointXN 3.0512 1.6599 1.838 0.0909 .
GenotypeWT:TimepointEN -5.4021 2.3474 -2.301 0.0401 *
GenotypeWT:TimepointXN -6.8777 2.3474 -2.930 0.0126 *

Signif. codes: 0 ā€˜ā€™ 0.001 ā€˜ā€™ 0.01 ā€˜ā€™ 0.05 ā€˜.ā€™ 0.1 ā€˜ ā€™ 1

Residual standard error: 2.033 on 12 degrees of freedom
Multiple R-squared: 0.7636, Adjusted R-squared: 0.665
F-statistic: 7.751 on 5 and 12 DF, p-value: 0.001825

TukeyHSD(aov(AtTotal_Model))
Tukey multiple comparisons of means
95% family-wise confidence level

Fit: aov(formula = AtTotal_Model)

$Genotype
diff lwr upr p adj
WT-pgm -4.804637 -6.89268 -2.716595 0.0003024

$Timepoint
diff lwr upr p adj
EN-ED -2.2247282 -5.356057 0.9066003 0.1822890
XN-ED -0.3876198 -3.518948 2.7437088 0.9419387
XN-EN 1.8371084 -1.294220 4.9684370 0.2973240

$Genotype:Timepoint
diff lwr upr p adj
WT:ED-pgm:ED -0.7113773 -6.286825 4.86407077 0.9976703
pgm:EN-pgm:ED 0.4763289 -5.099119 6.05177699 0.9996611
WT:EN-pgm:ED -5.6371626 -11.212611 -0.06171456 0.0469793
pgm:XN-pgm:ED 3.0512134 -2.524235 8.62666144 0.4792415
WT:XN-pgm:ED -4.5378302 -10.113278 1.03761788 0.1386769
pgm:EN-WT:ED 1.1877062 -4.387742 6.76315427 0.9762591
WT:EN-WT:ED -4.9257853 -10.501233 0.64966271 0.0954621
pgm:XN-WT:ED 3.7625907 -1.812857 9.33803871 0.2780518
WT:XN-WT:ED -3.8264529 -9.401901 1.74899516 0.2634488
WT:EN-pgm:EN -6.1134916 -11.688940 -0.53804351 0.0289673
pgm:XN-pgm:EN 2.5748844 -3.000564 8.15033249 0.6414669
WT:XN-pgm:EN -5.0141591 -10.589607 0.56128893 0.0875370
pgm:XN-WT:EN 8.6883760 3.112928 14.26382405 0.0022231
WT:XN-WT:EN 1.0993324 -4.476116 6.67478049 0.9829996
WT:XN-pgm:XN -7.5890436 -13.164492 -2.01359551 0.0065118
`

I just want to export this from the R console to a text file!

Thanks,

Erik

Wow, that copy and paste went horribly wrong..... sorry about that haha

Hi Sid-

Thanks so much for the suggestion. It worked perfectly. However, how do I switch back to the output appearing in the console? Right now when I give the summary() command it doesn't appear in the console.

Yes I have, and it ran in my console.

Thanks,

Erik

Can you please run sink.number() and see if there are a non-zero number of connections? If there, are, please call sink(file = NULL) until the number of reported connections are zero.

Hi-

Sorry, I ran the sink.number() code and nothing happened...

You should see output like this in the console.

> sink.number()
[1] 0
> 

yes, I see that now. Thanks! I think it has been fixed.

1 Like

Great! If I solved your problem, please consider marking my post as a solution.

I guess sink() will work as well instead of `sink(file=NULL).

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

You can use the sink() function in R. It will divert all R console output between the first and second call of sink() to a file that you specify. Here is an example using a linear model fitted to the iris data set.

fit <- lm(Petal.Length ~ Sepal.Length, data = iris)

sink(file = "lm_output.txt")
summary(fit)
sink(file = NULL)

This will create a file lm_output.txt in your working directory containing the output of summary(fit).

Hi Erik!

Have you considered using RMarkdown to make a word/pdf file with your results? I think in your case you'd only need a few code chunks with your model summary and your tukey results.

It isn't exactly what you asked but I hope it is somewhat helpful anyway :slight_smile:

1 Like

If all you need is to save your output for reference sink is a good option. I think this is a good place to introduce the package: broom. The broom package takes the messy output of built-in functions in R, such as lm , nls , or t.test , and turns them into tidy data frames. This is super useful if you want to use your coefficients or standard errors in further analyses.

https://cran.r-project.org/web/packages/broom/vignettes/broom.html

had you done this part ?

1 Like