[R] sink() not working as expected

Andrew Simmons @kw@|mmo @end|ng |rom gm@||@com
Tue Nov 2 19:18:10 CET 2021


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.

On Tue, Nov 2, 2021, 13:18 Rich Shepard <rshepard using appl-ecosys.com> wrote:

> 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
>
> ______________________________________________
> 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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list