[R] capturing the result of print in a variable?

Warnes, Gregory R gregory_r_warnes at groton.pfizer.com
Tue Sep 10 23:05:54 CEST 2002


Here are two small functions that I've been using lately.  They show how to
use textConnection to capture printed output:

texteval <- function( sourceText, collapse=NULL, echo=TRUE )
  {
    sourceConn <- textConnection(sourceText, open="r")
    resultConn <- textConnection("resultText", open="w")
    sink(resultConn)
    tmp <- source(file=sourceConn, local=FALSE, echo=echo, print.eval=TRUE)
    sink()
    close(resultConn)
    close(sourceConn)
    return( paste(c(resultText, collapse), collapse=collapse) )
    # the reason for c(result, collapse) is so that we get the line
    # terminator on the last line of output.  Otherwise, it just shows up
    # between the lines.
  }

printed <- function( sourceText, collapse=NULL )
  {
    return( texteval(sourceText, collapse, FALSE) )
  }


For example:

> printed(" x <- rnorm(100); y <- rnorm(100); print(summary( lm( y ~ x )))"
)
 [1] ""                                                            
 [2] "Call:"                                                       
 [3] "lm(formula = y ~ x)"                                         
 [4] ""                                                            
 [5] "Residuals:"                                                  
 [6] "    Min      1Q  Median      3Q     Max "                    
 [7] "-2.4206 -0.7519  0.1062  0.7153  2.3810 "                    
 [8] ""                                                            
 [9] "Coefficients:"                                               
[10] "            Estimate Std. Error t value Pr(>|t|)"            
[11] "(Intercept)  0.09799    0.10745   0.912    0.364"            
[12] "x           -0.00171    0.11397  -0.015    0.988"            
[13] ""                                                            
[14] "Residual standard error: 1.074 on 98 degrees of freedom"     
[15] "Multiple R-Squared: 2.298e-06,\tAdjusted R-squared: -0.0102 "
[16] "F-statistic: 0.0002252 on 1 and 98 DF,  p-value: 0.988 "     
[17] ""                                                            


-Greg




> -----Original Message-----
> From: Thomas Lumley [mailto:tlumley at u.washington.edu]
> Sent: Tuesday, September 10, 2002 3:45 PM
> To: Michael A. Miller
> Cc: r-help at stat.math.ethz.ch
> Subject: Re: [R] capturing the result of print in a variable?
> 
> 
> On 10 Sep 2002, Michael A. Miller wrote:
> 
> > I'd like to capture the results of a print() command so that I
> > can put the results in a tktext widget.  I know I can redirect
> > the output to a file with sink.  I'm looking for something like
> > that, but where I can redirect the output into a variable.  Is
> > there a way to do that?
> >
> Look at textConnection
> 
> 	-thomas
> 
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
> -.-.-.-.-.-.-.-.-
> r-help mailing list -- Read 
> http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: 
> r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
> _._._._._._._._._
> 


LEGAL NOTICE
Unless expressly stated otherwise, this message is confidential and may be privileged. It is intended for the addressee(s) only. Access to this E-mail by anyone else is unauthorized. If you are not an addressee, any disclosure or copying of the contents of this E-mail or any action taken (or not taken) in reliance on it is unauthorized and may be unlawful. If you are not an addressee, please inform the sender immediately.
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list