[R] bug in closing gzfile-opened connections?

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Jul 4 08:09:20 CEST 2007

Note that the use of read.table() does make a difference.  If you did

x <- scan(gzfile("xxx.gz"), list("","",""))

you would leave an unused connection, and showConnections(all=TRUE) would 
show this.  There is a finite pool of connections, and in general the 
correct way to use them is

con <- gzfile("xxx.gz")
x <- scan(con, list("","",""))

read.table() is the exception, so I suspect it is other things that have 
been done in the session that have used up the pool of connections.

On Tue, 3 Jul 2007, Duncan Murdoch wrote:

> On 03/07/2007 1:37 PM, David Reiss wrote:
>> Hi,
>> I am making multiple calls to gzfile() via read.table(), e.g.
>>> x <- read.table( gzfile( "xxx.gz" ) )
>> After i do this many times (I haven't counted, but probably between 50 and
>> 100 times) I get the error message:
>> Error in open.connection(file, "r") : unable to open connection
>> In addition: Warning message:
>> cannot open compressed file 'xxx.gz'
>> however, I also find that:
>>> showConnections()
>>      description class mode text isopen can read can write
>> so there are no (apparently) open connections. Calling closeAllConnections()
>> does not fix the problem. I have to quit and re-start R.
>> I am using R 2.5.0 on a Mac (OSX 10.4.9).
>> Anyone know if this is a bug or a 'feature'? I see from the gzfile help
>> that:
>>  In general functions using connections
>>      will open them if they are not open, but then close them again, so
>>      to leave a connection open call 'open' explicitly.
> You didn't give a reproducible example, so I couldn't say.  When I
> create a gzipped version of a write.table output and run
> for(i in 1:1000) read.table(gzfile(f))
> in R 2.5.0 I don't see a problem.  This is on Windows, but I doubt that
> makes a difference.
> Duncan Murdoch
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> 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.

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 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

More information about the R-help mailing list