Formatting summary() output to a file

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Formatting summary() output to a file

Rich Shepard
   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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Formatting summary() output to a file

R help mailing list-2
Try
   cat(sep="\n", file=con, capture.output(summary(...)))
capture.output(x) return character vector whose elements contain
the lines of text that would have been printed by print(x).

Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Fri, Jul 27, 2018 at 8:20 AM, Rich Shepard <[hidden email]>
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
>
> ______________________________________________
> [hidden email] 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/posti
> ng-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Formatting summary() output to a file

Bert Gunter-2
In reply to this post by Rich Shepard
Not quite sure what you mean here.

R is open source, so

> print.summaryDefault  ## at the prompt. It's in base R, so no package::
prefix needed

will give you the code used for formatting. You can then do the same.

Cheers,
Bert







Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Fri, Jul 27, 2018 at 8:20 AM, Rich Shepard <[hidden email]>
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
>
> ______________________________________________
> [hidden email] 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/posti
> ng-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Formatting summary() output to a file

Rich Shepard
In reply to this post by R help mailing list-2
On Fri, 27 Jul 2018, William Dunlap wrote:

> Try
>   cat(sep="\n", file=con, capture.output(summary(...)))
> capture.output(x) return character vector whose elements contain
> the lines of text that would have been printed by print(x).

Bill,

   Thanks very much. I doubt my searches would have found capture.output().

Rich

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Formatting summary() output to a file

Rich Shepard
In reply to this post by Bert Gunter-2
On Fri, 27 Jul 2018, Bert Gunter wrote:

> print.summaryDefault  ## at the prompt. It's in base R, so no package::
> prefix needed will give you the code used for formatting. You can then do
> the same.

Bert,

   Thank you.

Rich

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Formatting summary() output to a file

R help mailing list-2
In reply to this post by Rich Shepard
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" <[hidden email] on behalf of [hidden email]> 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
   
    ______________________________________________
    [hidden email] 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.
   

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Formatting summary() output to a file

Rich Shepard
On Fri, 27 Jul 2018, MacQueen, Don wrote:

> Given your description, I would start with
>
> sink('wysumallyrs.txt')
> print( summary(wyallyrs) )
> sink()
>
> and see if that doesn't meet your needs.

Don,

   I started with sink() trying to follow
<https://stat.ethz.ch/R-manual/R-patched/library/base/html/sink.html> (which
is the same as ?sink within R) using the two examples at the bottom. Neither
used the print() function and the results were an empty file (using example
1) or an error message about incorrect usage (using example 2).

   Your solution does make more sense than does using cat().

Thanks,

Rich

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Formatting summary() output to a file

R help mailing list-2
In reply to this post by Rich Shepard
I often use capture.output to slightly reformat printout output.  E.g, to
indent str's output to make it easier to read debugging printouts:
debug_print <- function(x, name=substitute(x), indent=4)
{
   cat(sep="\n", name, paste0(strrep(" ", indent), capture.output(str(x))))
}
Used as in
> myData <- list(One=1:100,Two=log2(1:5))
> debug_print(myData)
myData
    List of 2
     $ One: int [1:100] 1 2 3 4 5 6 7 8 9 10 ...
     $ Two: num [1:5] 0 1 1.58 2 2.32


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Fri, Jul 27, 2018 at 8:52 AM, Rich Shepard <[hidden email]>
wrote:

> On Fri, 27 Jul 2018, William Dunlap wrote:
>
> Try
>>   cat(sep="\n", file=con, capture.output(summary(...)))
>> capture.output(x) return character vector whose elements contain
>> the lines of text that would have been printed by print(x).
>>
>
> Bill,
>
>   Thanks very much. I doubt my searches would have found capture.output().
>
>
> Rich
>
> ______________________________________________
> [hidden email] 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/posti
> ng-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Formatting summary() output to a file

Rich Shepard
On Fri, 27 Jul 2018, William Dunlap wrote:

> I often use capture.output to slightly reformat printout output.  E.g, to
> indent str's output to make it easier to read debugging printouts:
> debug_print <- function(x, name=substitute(x), indent=4)
> {
>   cat(sep="\n", name, paste0(strrep(" ", indent), capture.output(str(x))))
> }
> Used as in
>> myData <- list(One=1:100,Two=log2(1:5))
>> debug_print(myData)
> myData
>    List of 2
>     $ One: int [1:100] 1 2 3 4 5 6 7 8 9 10 ...
>     $ Two: num [1:5] 0 1 1.58 2 2.32

Bill,

   This is good to know.

Many thanks,

Rich

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Formatting summary() output to a file

R help mailing list-2
In reply to this post by Rich Shepard
Hmmm. I do get output in the file with the first example, and the second example is too complicated for me.
-Don

--
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, 9:56 AM, "R-help on behalf of Rich Shepard" <[hidden email] on behalf of [hidden email]> wrote:

    On Fri, 27 Jul 2018, MacQueen, Don wrote:
   
    > Given your description, I would start with
    >
    > sink('wysumallyrs.txt')
    > print( summary(wyallyrs) )
    > sink()
    >
    > and see if that doesn't meet your needs.
   
    Don,
   
       I started with sink() trying to follow
    <https://stat.ethz.ch/R-manual/R-patched/library/base/html/sink.html> (which
    is the same as ?sink within R) using the two examples at the bottom. Neither
    used the print() function and the results were an empty file (using example
    1) or an error message about incorrect usage (using example 2).
   
       Your solution does make more sense than does using cat().
   
    Thanks,
   
    Rich
   
    ______________________________________________
    [hidden email] 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.
   

______________________________________________
[hidden email] 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.