[R] connections: remember to close it!
Prof Brian Ripley
ripley at stats.ox.ac.uk
Sat Oct 6 18:17:01 CEST 2001
On Tue, 2 Oct 2001, Liaw, Andy wrote:
> Hi all,
>
> I have newly discovered the power of connections (although just a small part
> of it, namely the file() connection). It allowed me to process data file
> with arbitrary number of fields per line, which I was using awk for
> preprocessing before readling into R. The combination of the connection,
> readLines() and strsplit() really make the work a lot easier and more
> organized. Thanks a bundle to the R Core for this excellent work!!
>
> One reminder for people new to connections: If you open connections inside
> a function, make sure they are closed before exiting from that function,
> otherwise you end up with connections that can not be closed (or at least I
> tried closeAllConnections() without success on WinNT4). Not being very
> smart or clear-headed, I turned a working R script that opens the connection
> and process the data into an R function, but introduced some bugs in the
> process. I forgot to add "on.exit(close(filecon))" right after I open the
> connection in the function. As a result, repeated failed calls to the
> function generated a bunch of open connections that I can't close. I'm not
> sure what the consequences of this is. I was able to quit R gracefully, but
> my NT seemed to have become unstable afterward.
Some terminology:
`close' closes and destroys a connection.
`closeAllConnections' closes all open user connections.
Notice the distinction.
You can close and destroy connections later. Just call
showConnections(all=FALSE) and then use
close(getConnection(some.number)). This *is* on the *same* help page as
closeAllConnections.
closeAllConnections was broken when close was made generic. It should read
function ()
{
set <- getAllConnections()
set <- set[set > 2]
for (i in seq(along = set)) close(getConnection(set[i]))
invisible()
}
and needs to be amended to handle the sink stack.
--
Brian D. Ripley, ripley at 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-help 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-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list