[R] sink() not working as expected

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Tue Nov 2 19:58:00 CET 2021


What do you think these 2 lines are doing?

cat ('corvalis discharge summary\n')
print(cat)

Please consult ?cat . You might also spend a bit of (more?) time with an R
tutorial or two as you seem confused about how assignment (<-) works. Or
maybe I'm confused about what is confusing you....

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 Tue, Nov 2, 2021 at 11:46 AM Rich Shepard <rshepard using appl-ecosys.com>
wrote:

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