[R] sink() not working as expected

Rich Shepard r@hep@rd @end|ng |rom @pp|-eco@y@@com
Tue Nov 2 19:45:41 CET 2021


On Tue, 2 Nov 2021, Andrew Simmons wrote:

> You probably want to use cat and print for these lines. These things won't
> print when not run at the top level, so if you want them to print, you must
> specify that.

Andrew,

I modified the file to this:
sink('data-summaries.txt')
cat ('corvalis discharge summary\n')
print(cat)
summary(cor_disc)
sd(cor_disc$cfs)
cat ('-----------------\n')
print(cat)
sink()

and ran it from the R command line:
> sink('data-summaries.txt')
> cat ('corvalis discharge summary\n')
> print(cat)
> summary(cor_disc)
> sd(cor_disc$cfs)
> cat ('-----------------\n')
> print(cat)
> sink()
>

with this output:
corvalis discharge summary
function (..., file = "", sep = " ", fill = FALSE, labels = NULL,
     append = FALSE) 
{
     if (is.character(file))
         if (file == "")
             file <- stdout()
         else if (startsWith(file, "|")) {
             file <- pipe(substring(file, 2L), "w")
             on.exit(close(file))
         }
         else {
             file <- file(file, ifelse(append, "a", "w"))
             on.exit(close(file))
         }
     .Internal(cat(list(...), file, sep, fill, labels, append))
}
<bytecode: 0xbf3730>
<environment: namespace:base>
    site_nbr              year           mon              day
  Length:415263      Min.   :2009   Min.   : 1.000   Min.   : 1.00
  Class :character   1st Qu.:2012   1st Qu.: 4.000   1st Qu.: 8.00
  Mode  :character   Median :2015   Median : 7.000   Median :16.00
                     Mean   :2015   Mean   : 6.552   Mean   :15.74
                     3rd Qu.:2018   3rd Qu.:10.000   3rd Qu.:23.00
                     Max.   :2021   Max.   :12.000   Max.   :31.00
        hr             min             tz                 cfs
  Min.   : 0.00   Min.   : 0.00   Length:415263      Min.   :1000
  1st Qu.: 5.00   1st Qu.: 0.00   Class :character   1st Qu.:1950
  Median :11.00   Median :15.00   Mode  :character   Median :4740
  Mean   :11.49   Mean   :22.44                      Mean   :4624
  3rd Qu.:17.00   3rd Qu.:30.00                      3rd Qu.:6580
  Max.   :23.00   Max.   :55.00                      Max.   :9990 
[1] 2600.546
-----------------
function (..., file = "", sep = " ", fill = FALSE, labels = NULL,
     append = FALSE) 
{
     if (is.character(file))
         if (file == "")
             file <- stdout()
         else if (startsWith(file, "|")) {
             file <- pipe(substring(file, 2L), "w")
             on.exit(close(file))
         }
         else {
             file <- file(file, ifelse(append, "a", "w"))
             on.exit(close(file))
         }
     .Internal(cat(list(...), file, sep, fill, labels, append))
}
<bytecode: 0xbf3730>
<environment: namespace:base>

Much closer, but still not quite right. How should I modify this?

Thanks,

Rich



More information about the R-help mailing list