How to close unused connections after reading html in R

I'm new to R and trying to access some information on the Internet, but I have connection problems that don't seem to close. I would really appreciate it if someone here could give me advice ...

Originally, I wanted to use the WebChem package, which theoretically delivers everything I want, but when some output is not on the web page, WebChem does not return any data from this page. To get around this, I took most of the code from the package, but slightly modified it to fit my needs. This worked fine, for about the first 150 usages, but now, although I haven’t changed anything when I use the read_html command, I get a warning message β€œclose unused connection 4 (http: ....." Although this is only a warning message, read_html returns nothing after creating this warning.

I wrote a simplified code below. This has the same problem.

Closing R completely (or even rebooting my PC) does not seem to matter much - now a warning message appears the second time I use the code. I can run queries one at a time outside the loop without any problems, but as soon as I try to use the loop, the error will repeat in the second iteration. I tried to draw code in code, and again it returned the same error message. I tried showConnections (all = TRUE), but only got 0-2 connections for stdin, stdout, stderr. I tried to find ways to close the html connection, but I cannot define the url as con, and close (qurl) and close (ttt) also do not work. (Return errors of an inapplicable method for "close" applied to an object of class "character", and no applicable method for "close" applied to an object of class "c (" xml_document "," xml_node "), respectively)

Does anyone know a way to close these connections so that they don't break my routine? Any suggestions would be greatly appreciated. Thanks!

PS: I am using R version 3.3.0 with RStudio version 0.99.902.

CasNrs <- c("630-08-0","463-49-0","194-59-2","86-74-8","148-79-8") tit = character() for (i in 1:length(CasNrs)){ CurrCasNr <- as.character(CasNrs[i]) baseurl <- 'http://chem.sis.nlm.nih.gov/chemidplus/rn/' qurl <- paste0(baseurl, CurrCasNr, '?DT_START_ROW=0&DT_ROWS_PER_PAGE=50') ttt <- try(read_html(qurl), silent = TRUE) tit[i] <- xml_text(xml_find_all(ttt, "//head/title")) } 
+6
source share

All Articles