Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I correctly close a connection in R, so its connection 'slot' gets released?

Tags:

r

base

connection

I am using readLines(text url) in a script, where readLines(text url) is called several hundred times, where each text url is unique.

After about 125 calls to readLines(text url) I got an error, "all connections are in use."

When I check my open connections with showConnections(all=TRUE), for the url connections I see:

 description     class ... isopen
 "www.site.com"  "url" ... "closed" ...

How do I remove these closed connections from the R environment so I can open new connections?

Also, I've tried opening the urls before hand, passing the url connection into readLines, then closing the connection after I'm done with the connection, and still run into the same problem.

like image 658
user220419 Avatar asked Nov 23 '13 23:11

user220419


1 Answers

The easiest way to avoid problems like this is to explicitly close the connection when you're done with it. In R, the easiest way to do that is to use on.exit() which will ensure the url gets closed even if an error occurs in your code

read_url <- function(url, ...) {
  on.exit(close(url))
  readLines(url, ...)
}
showConnections()
g <- read_url("http://www.google.com")
showConnections()
like image 92
hadley Avatar answered Oct 24 '22 07:10

hadley