[R] Warning message about closing a connection XXXX

Duncan Murdoch murdoch.duncan at gmail.com
Wed Jan 4 19:21:14 CET 2012


On 12-01-04 12:25 PM, Prof Brian Ripley wrote:
> On 04/01/2012 17:12, William Dunlap wrote:
>> Re
>>     >   How can I avoid the warning message altogether?
>>
>>     ?closeAllConnections
>>
>> I think of calls to closeAllConnections() in the same
>> way that I think of calls to rm(list=objects()):
>> they both can remove things that are not theirs to remove.
>>
>> Calling gc() will close all unused connections, so
>> no damage can be done.  (I thought that the warning
>> about closing unused text connections was dropped in
>> a recent release of R so neither is really needed.)
>
> Yes, it was.  The warning is there because the user may need to do
> something about the 'other end' of the now-closed connection, which R
> manages for textConnections.
>
> There is however one circumstance where gc() does not close all unused
> connections, and that is when gzcon() is used (because that involves two
> connections and it is not clear when the inner one is 'in use': and we
> used to guess wrong).
>
> Rather than using a sledgehammer, use showConnections(all=TRUE) to see
> all connections, and close the ones you want to (and its help page shows
> you how).

In older versions that do give the warning, wouldn't it be sufficient to 
name the textConnection, and close it explicitly?  E.g.

  mydata<- read.table(con <- textConnection(mystring),  header=TRUE, 
sep=",",   row.names="id", na.strings=" ")

  close(con)

Duncan Murdoch


>
>> Bill Dunlap
>> Spotfire, TIBCO Software
>> wdunlap tibco.com
>>
>>> -----Original Message-----
>>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of David Winsemius
>>> Sent: Wednesday, January 04, 2012 7:10 AM
>>> To: Dan Abner
>>> Cc: r-help at r-project.org
>>> Subject: Re: [R] Warning message about closing a connection XXXX
>>>
>>>
>>> On Jan 4, 2012, at 9:53 AM, Dan Abner wrote:
>>>
>>>> Hello everyone,
>>>>
>>>> After running the following code, I obtain this error message.
>>>>
>>>>
>>>>> mydata<- read.table(textConnection(mystring),
>>>> +    header=TRUE, sep=",",
>>>> +    row.names="id", na.strings=" ")
>>>>> mydata
>>>> Warning message:
>>>> closing unused connection 3 (mystring)
>>>>
>>>> =====
>>>>
>>>> However, when I attempt to run read.table() again and immediately
>>>> submit
>>>> the close() function (below), I obtain this message:
>>>>
>>>>> close(mystring)
>>>> Error in UseMethod("close") :
>>>>    no applicable method for 'close' applied to an object of class
>>>> "character"
>>>> =====
>>>>
>>>> How can I avoid the warning message altogether?
>>>
>>> ?closeAllConnections
>>>
>>> The error message occurs because "mystring was never the connection
>>> name.
>>>
>>> --
>>>
>>> David Winsemius, MD
>>> West Hartford, CT
>>>
>>> ______________________________________________
>>> R-help at r-project.org 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.
>>
>> ______________________________________________
>> R-help at r-project.org 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.
>
>



More information about the R-help mailing list