[R] immortal connections

Prof Brian D Ripley ripley at stats.ox.ac.uk
Fri Feb 15 09:20:03 CET 2002


On Thu, 14 Feb 2002, Pierre Kleiber wrote:

>   I have several data input functions that include a construct like
> the following:
>
>    indat <- function(stuff) {
>      [...]
>      awkp <- paste("awk ...yada yada...", stuff, etc.)
>      scan(pipe(awkp),quiet=TRUE)
>    }
>
> I use these functions a lot, and as I do, a lengthening list of
> closed, but undestroyed, connections accumulates.  Ultimately this
> appears to stuff things up and cause error messages, such as "cannot
> close output sink connection".  I seem to remember a related query
> some time ago on this list, but I can't locate it.  So my question is:
> Should I include a closeAllConnections() at the end of each input
> function?... or is there a preferrable tactic?

That's how connections work.  You need to be using

   awkp <- paste("awk ...yada yada...", stuff, etc.)
   con <- pipe(awkp)
   scan(con, quiet=TRUE)
   close(con)

When passed a connection, scan can open and close it, but it cannot destroy
it as it does not know if it is the sole user.  There is no way for scan to
distinguish between

scan(pipe(awkp),quiet=TRUE)
scan((con <- pipe(awkp)), quiet=TRUE)

There is a finite pool (about 50) of connections, and you will eventually
run out.  That does not `stuff things up', just stop you creating any more
connections.

It would possible to be clever and reclaim unused connections, but for the
fact that the Green Book seems to suggest you can legitimately go to
showConnections() and reclaim them by getConnection(a number).

-- 
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