Skip to content
Prev 26724 / 63421 Next

isOpen on closed connections

"Roger D. Peng" <rpeng at jhsph.edu> writes:
I see this too with R-devel (r43376) {from Nov 6th}.

    con = file("example1", "w")
    isOpen(con)

    [1] TRUE

    showConnections()

      description class  mode text   isopen   can read can write
    3 "example1"  "file" "w"  "text" "opened" "no"     "yes"    

    close(con)
    isOpen(con)

    Error in isOpen(con) : invalid connection

    ## printing also fails
    con
    Error in summary.connection(x) : invalid connection
This doesn't address isOpen, but why do you have the call to close
inside the tryCatch block?  Isn't the idea that finally will always be
run and so you can be reasonably sure that close gets called once?

If your real world code is more complicated, perhaps you can make use
of a work around like:

myIsOpen = function(con) tryCatch(isOpen(con), error=function(e) FALSE)

You could do similar with myClose and "close" a connection as many
times as you'd like :-)

+ seth