Re: [R] how to close connections?

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Thu 25 Aug 2005 - 19:48:45 EST

Try this

con <- gzfile("file.gz")
a <- readLines(con, n=1)
showConnections(all=TRUE)
close(con)
showConnections(all=TRUE)

So the connection is opened and closed as it says, but not destroyed. You do need to call close() explicitly -- from the help file

      'close' closes and destroys a connection.
                     ^^^^^^^^^^^^

There are 'create - open - close - destroy' states for a connection.

On Thu, 25 Aug 2005, Ott Toomet wrote:

> Hi everybody,
>
> I am using gzipped files quite extensively and cannot figure out how I
> can close (or reuse) connections.
>
> Example:
>
>> for(i in 1:300) {cat(i, "\n"); a <- readLines(gzfile("file.gz"), n=1) }
> 1
> 2
> ...
> 46
> 47
> Error in gzfile("~/tyyq/andmebaasiq/ETU/ETU1997.tsv.gz") :
> all connections are in use
>
> To use just gzfile without any open/close should be OK as the help
> states:
>
> ... 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.
>
> I have also experimented with explicit open/close but without any more
> success, though, R claims that the connections will be closed.
>
> Any ideas? Is it my misunderstanding? What exactly means a
> connection is "in use"?

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

______________________________________________
R-help@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
Received on Thu Aug 25 19:53:44 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 15:50:39 EST