Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - capturing elements of R output into text files

Tags:

I am trying to run an analysis by invoking R through the command line as follows:

R --no-save < SampleProgram.R > SampleProgram.opt 

For example, consider the simple R program below:

mydata = read.csv("test.txt", header=T) attach(mydata) summary(Variable1) q() 

The output is displayed in SampleProgram.opt (only partially shown):

> mydata = read.csv("test.txt", header=T) > attach(mydata) > summary(Variable1)    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    1.00    1.00    2.00    2.47    3.00    4.00 > q() 

This simple R program is going to be executed by a script that needs to use the summary statistics displayed for Variable1.

The question is this: Is there any way in R to capture the output of summary(Variable1) and write the results into an output file? In other words, I need R to run the summary statistics for Variable1, capture the "Min", "Median" and "Max" values and write those alone to an output text file. In this example, the output file should contain only one line with the values "1.00, 2.00, 4.00" (i.e. the "Min", "Median" and "Max" values).

The example above talks about the summary function. But, I need to do that with other commands as well (such as glm)

I am fairly new to R and was wondering if there was a way in R that I could do this?

Thanks for the help.

like image 610
Karthik Avatar asked Nov 14 '09 17:11

Karthik


People also ask

How do I save an R output to a text file?

You can also save the entire R console screen within the GUI by clicking on "Save to File..." under the menu "File." This saves the commands and the output to a text file, exactly as you see them on the screen.

How do I export a DataFrame to a text file in R?

Use R base function wirte. table() to export the data from R DataFrame to a text file. Besides this, R also provides a third-party package to write the text file.

How do you write data to a file in R?

In R, we can write data frames easily to a file, using the write. table() command. The first argument refers to the data frame to be written to the output file, the second is the name of the output file. By default R will surround each entry in the output file by quotes, so we use quote=F.


2 Answers

A simple way is to convert the output that you want to print to file, and convert it to a text string via capture.output. then you can simply cat the output to the file.

dat<-data.frame(a=rnorm(100),b=rnorm(100),c=rnorm(100)) mod<-lm(a~b+c,data=dat) out<-capture.output(summary(mod)) cat(out,file="out.txt",sep="\n",append=TRUE) out<-capture.output(vcov(mod)) cat(out,file="out.txt",sep="\n",append=TRUE) 

this creates a file out.txt containing

Call: lm(formula = a ~ b + c, data = dat)  Residuals:      Min       1Q   Median       3Q      Max  -2.67116 -0.81736 -0.07006  0.76551  2.91055   Coefficients:             Estimate Std. Error t value Pr(>|t|) (Intercept)  0.01196    0.11724   0.102    0.919 b            0.11931    0.12601   0.947    0.346 c           -0.09085    0.13267  -0.685    0.495  Residual standard error: 1.171 on 97 degrees of freedom Multiple R-squared: 0.0183, Adjusted R-squared: -0.001944  F-statistic: 0.9039 on 2 and 97 DF,  p-value: 0.4084                 (Intercept)             b             c (Intercept)  0.0137444761 -0.0006929722 -0.0005721338 b           -0.0006929722  0.0158784141  0.0042188705 c           -0.0005721338  0.0042188705  0.0176018744 
like image 110
Ian Fellows Avatar answered Sep 27 '22 18:09

Ian Fellows


There are many ways:

  • use sink()
  • open a file via file() and write results to it
  • place your code in a file and run it via R CMD BATCH file.R which creates output
  • explicitly write results data via write.table() or its variants like write.csv()

This is fairly elementary so you will probably benefit from reading the 'Introduction to R' manual, or one of the numerous books on R.

The simplest solution may be

R> X <- rnorm(100) R> summary(X)    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.   -2.480  -0.618  -0.223  -0.064   0.609   2.440  R> write.table(matrix(summary(X)[c(1,3,6)], nrow=1), \                file="/tmp/foo.txt", col.names=FALSE, row.names=FALSE) R> system("cat /tmp/foo.txt") -2.48 -0.223 2.44 R>  

where I force the subset of summary() to be a matrix of one row.

like image 24
Dirk Eddelbuettel Avatar answered Sep 27 '22 20:09

Dirk Eddelbuettel