From sfalcon at fhcrc.org  Wed Sep 12 16:05:28 2007
From: sfalcon at fhcrc.org (Seth Falcon)
Date: Wed, 12 Sep 2007 07:05:28 -0700
Subject: [Rd] warning upon automatic close of connection
In-Reply-To: <971536df0709120521t6f7406fcic41a5e3874c488ec@mail.gmail.com>
	(Gabor Grothendieck's message of "Wed\,
	12 Sep 2007 08\:21\:06 -0400")
References: <971536df0709120521t6f7406fcic41a5e3874c488ec@mail.gmail.com>
Message-ID: <m2odg8m11z.fsf@fhcrc.org>

"Gabor Grothendieck" <ggrothendieck at gmail.com> writes:
> I noticed that under R 2.6.0 there is a warning about closing the connection
> in the code from this post:
> https://stat.ethz.ch/pipermail/r-help/2007-September/140601.html
>
> which is evidently related to the following from the NEWS file:
>
>     o	Connections will be closed if there is no R object referring to
> 	them.  A warning is issued if this is done, either at garbage
> 	collection or if all the connection slots are in use.
>
> If we use read.table directly it still happens:
>
> # use Lines and Lines2 from cited post
> library(zoo)
> DF1 <- read.table(textConnection(Lines), header = TRUE)
> DF2 <- read.table(textConnection(Lines2), header = TRUE)
> z1 <- zoo(as.matrix(DF1[-1]), as.Date(DF1[,1], "%d/%m/%Y"))
> z2 <- zoo(as.matrix(DF2[-1]), as.Date(DF2[,1], "%d/%m/%Y"))
> both <- merge(z1, z2)
> plot(na.approx(both))
>
>> R.version.string # Vista
> [1] "R version 2.6.0 alpha (2007-09-06 r42791)"
>
> Is this annoying warning really necessary?  I assume we can get rid of
> it by explicitly naming and closing the connections but surely there should
> be a way to avoid the warning without going to those lengths.

Up until the change you mention above it really was necessary to name
and close all connections.  Short scripts run in fresh R sessions may
not have had problems with code like you have written above, but
longer programs or shorter ones run in a long running R session would
run out of connections.

Now that connections have weak reference semantics, one can ask
whether this behavior should be "standard" and no warning issued.

> I would have thought that read.table opens the connection then it would
> close it itself so no warning would need to be generated.

In your example, read.table is _not_ opening the connection.  You are
passing an open connection which has no symbol bound to it:

   foo = ""
   c = textConnection(foo)
   c
     description            class             mode             text 
           "foo" "textConnection"              "r"           "text" 
          opened         can read        can write 
        "opened"            "yes"             "no" 

But I think passing a closed connection would cause the same sort of
issue.  It seems that there are two notions of "closing a connection":
(i) close as the opposite of open, and (ii) clean up the entire
connection object.  I haven't looked closely at the code here, so I
could be wrong, but I'm basing this guess on the following:

> file("foo")
description       class        mode        text      opened    can read 
      "foo"      "file"         "r"      "text"    "closed"       "yes" 
  can write 
      "yes" 
## start new R session
for (i in 1:75) file("foo")
gc()
warnings()[1:3]
> gc()
         used (Mb) gc trigger (Mb) max used (Mb)
Ncells 149603  4.0     350000  9.4   350000  9.4
Vcells 101924  0.8     786432  6.0   486908  3.8
There were 50 or more warnings (use warnings() to see the first 50)
> warnings()[1:3]
$`closing unused connection 76 (foo)`
NULL

$`closing unused connection 75 (foo)`
NULL

$`closing unused connection 74 (foo)`
NULL


-- 
Seth Falcon | Computational Biology | Fred Hutchinson Cancer Research Center
BioC: http://bioconductor.org/
Blog: http://userprimary.net/user/



[Rd] warning upon automatic close of connection
Seth Falcon