[Rd] isOpen on closed connections

Roger D. Peng rpeng at jhsph.edu
Wed Nov 14 15:33:29 CET 2007


As far as I can tell, 'isOpen' cannot return FALSE in the case when 'rw = ""'. 
If the connection has already been closed by 'close' or some other function, 
then isOpen will produce an error.  The problem is that when isOpen calls 
'getConnection', the connection cannot be found and 'getConnection' produces an 
error.  The check to see if it is open is never actually done.

This came up in some code where I'm trying to clean up connections after 
successfully opening them.  The problem is that if I try to close a connection 
that has already been closed, I get an error (because 'getConnection' cannot 
find it).  But then there's no way for me to find out if a connection has 
already been closed.  Perhaps there's another approach I should be taking?  The 
context is basically,

con <- file("foo", "w")

tryCatch({
	## Do stuff that might fail
	writeLines(stuff, con)
	close(con)

	file.copy("foo", "bar")
}, finally = {
	close(con)
})

So the problem is that if the block in the 'tryCatch' succeeds, the 'finally' 
will produce an error.

I'm not exactly sure of what I'd want since it seems modifying 'getConnection' 
would not be a great idea as it is used elsewhere.

-roger
-- 
Roger D. Peng  |  http://www.biostat.jhsph.edu/~rpeng/



More information about the R-devel mailing list