[Rd] Problem with sink() in 1.20 on Windows (PR#779)

Prof Brian D Ripley ripley@stats.ox.ac.uk
Tue, 19 Dec 2000 20:36:46 +0000 (GMT)

On Tue, 19 Dec 2000 Setzer.Woodrow@epamail.epa.gov wrote:

> When I use sink(filename) to save the results of the run, the file
> does not seem to be closed or released back to the OS after I issue
> the "sink()" command.  Here is a baby example:

sink works on connections, not files, as from 1.2.0.

> sink("test.txt")
> tdta <- data.frame(x = x <- 0:10,y = 2 * x + rnorm(11))
> print(summary(lm(y ~ x, data=tdta)))
> sink()
> At this point (even after refreshing the Explorer window) the filesize of
> "test.txt" is listed as 0 bytes, and as long as Rgui is running, I
> cannot delete the file.  Emacs cannot read the file (but Notepad can).  This happens both in the gui
> and when the commands are in a file run via "source()".

Yes, but what is the bug exactly?  The connection is still open so you can
switch output back to it.  If you want different behaviour, you can open a
connection and close it yourself.  As in

zz <- file("test.txt", "w")
tdta <- data.frame(x = x <- 0:10,y = 2 * x + rnorm(11))
print(summary(lm(y ~ x, data=tdta)))

It might be better to close the file as well as the connection when
switching, but that is not what was done.  To do that use

sink <- function (file = NULL, append = FALSE) 
    if (is.null(file)) 
        file <- 1
    else if (is.character(file)) { 
        file <- file(file, ifelse(append, "a", "w"))
    } else if (!inherits(file, "connection")) 
        stop("`file' must be NULL, a connection or a character string")
    .Internal(sink(file, append))

(One possible cause of confusion is that the connection might be closed,
but not the file.)

Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch