[R] sink() not working as expected

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


I've read ?sink and several web pages about it but it's not working properly
when I have the commands in a script and source() them.

The file:
library(tidyverse)
library(lubridate)

sink('data-summaries.txt')
'corvalis discharge summary\n'
summary(cor_disc)
sd(cor_disc$cfs)
'-----------------\n'
sink()

> source('summary_stats.R')
results in an empty file.

When I enter them line-by-line on the R command line I get output including
multiple lines listing the PWD and ignoring the newline:

[1] "/path/to/PWD"
[1] "corvalis discharge summary\n"
[1] "/path/to/PWD"
    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] "/path/to/PWD"
[1] 2600.546
[1] "/path/to/PWD"
[1] "-----------------\n"
[1] "/path/to/PWD"
~

What am I doing incorrectly here? I've looked at examples on web pages
without learning where I'm erring.

TIA,

Rich



More information about the R-help mailing list