[R] Formatting summary() output to a file

MacQueen, Don m@cqueen1 @end|ng |rom ||n|@gov
Fri Jul 27 18:40:49 CEST 2018


Given your description, I would start with

sink('wysumallyrs.txt')
print( summary(wyallyrs) )
sink()

and see if that doesn't meet your needs.

Some of the basic principles:

(1) Whenever you type the name of an R object at the R prompt, it is as if R wraps whatever you typed inside print(). Here are some examples to illustrate this

> 3
[1] 3
> print(3)
[1] 3
> sqrt(2)
[1] 1.414214
> print(sqrt(2))
[1] 1.414214
> dtf <- data.frame(x=1:4, y=rnorm(4))
> dtf
  x            y
1 1  0.493453813
2 2 -0.586864827
3 3  2.481334630
4 4 -0.007107974
> print(dtf)
  x            y
1 1  0.493453813
2 2 -0.586864827
3 3  2.481334630
4 4 -0.007107974
> lm(y~x, dtf)

Call:
lm(formula = y ~ x, data = dtf)

Coefficients:
(Intercept)            x  
     0.2036       0.1567  

> print( lm(y~x, dtf) )

Call:
lm(formula = y ~ x, data = dtf)

Coefficients:
(Intercept)            x  
     0.2036       0.1567  

>

In every case, the output is identical. 

Note that when making an assignment, as in
   dtf <- data.frame(x=1:4, y=rnorm(4))
there is no automatic printing


(2) print() and cat() are not the same thing

> cat( lm(y~x, dtf) )
Error in cat(lm(y ~ x, dtf)) : 
  argument 1 (type 'list') cannot be handled by 'cat'

print() generally knows how to display complex objects in a nice format/layout, cat() does not. cat() is designed for a different purpose.

If you want to start learning how it is that print() know how to format complex objects, you could start with
> ?print.data.frame
> ?print.default
> ?print


(3) Note that if you use sink(), print(), sink() the way I suggested, then subsequently you can append to the file using

sink('wysumallyrs.txt', append=TRUE)
{print or cat or whatever}
sink()

--
Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
Lab cell 925-724-7509
 
 

On 7/27/18, 8:20 AM, "R-help on behalf of Rich Shepard" <r-help-bounces using r-project.org on behalf of rshepard using appl-ecosys.com> wrote:

       I want to save the output of summary(df_name) to a disk file and my
    research found that the sink() function is what I want to use. The 'R Cookbook'
    provides a an alternative example using cat() to a connection. Here,
    
    con <- file("wysumallyrs.txt", "w")
    cat(summary(wyallyrs), file=con)
    close(con)
    
    produces this output:
    
    Length:402531      Class :character   Mode  :character   NA NA NA NA
    Length:402531      Class :character   Mode  :character   NA NA NA NA Min.
    :90.65   1st Qu.:93.81   Median :94.14   Mean   :93.86   3rd Qu.:94.43
    Max. :98.91   NA's   :225   Min. :1988-10-01   1st Qu.:1996-02-01   Median
    :2001-12-01   Mean   :2002-07-28   3rd Qu.:2008-09-10   Max. :2018-06-21
    NA Min. :1988-10-01 00:30:00   1st Qu.:1996-02-01 00:45:00   Median
    :2001-12-01 15:30:00   Mean   :2002-07-29 03:04:28   3rd Qu.:2008-09-10
    16:00:00   Max. :2018-06-21 00:00:00   NA
    
       Is there a way to format this output as it is on the console when the
    script contains
    
    sum <- summary(wyallyrs)
    print(sum)
    
          date               time                elev           myDate
      Length:402531      Length:402531      Min.   :90.65   Min.   :1988-10-01
      Class :character   Class :character   1st Qu.:93.81   1st Qu.:1996-02-01
      Mode  :character   Mode  :character   Median :94.14   Median :2001-12-01
                                            Mean   :93.86   Mean   :2002-07-28
                                            3rd Qu.:94.43   3rd Qu.:2008-09-10
                                            Max.   :98.91   Max.   :2018-06-21
                                            NA's   :225
          myTime
      Min.   :1988-10-01 00:30:00
      1st Qu.:1996-02-01 00:45:00
      Median :2001-12-01 15:30:00
      Mean   :2002-07-29 03:04:28
      3rd Qu.:2008-09-10 16:00:00
      Max.   :2018-06-21 00:00:00
    
    TIA,
    
    Rich
    
    ______________________________________________
    R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
    https://stat.ethz.ch/mailman/listinfo/r-help
    PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
    and provide commented, minimal, self-contained, reproducible code.
    



More information about the R-help mailing list