[R] connections: remember to close it!

Liaw, Andy andy_liaw at merck.com
Tue Oct 2 14:44:35 CEST 2001

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.

System info:  R-1.3.1 on WinNT4sp6.  IBM Thinkpad 600E with PII and 128MB


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