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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With