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
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 <rshepard at 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 at 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/posti > ng-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
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 <rshepard at 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 at 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/posti > ng-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
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
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
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)) > dtfx 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 at r-project.org on behalf of rshepard at 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 at 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.
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