Hello; I am new to R and trying to read a line from socket connection at a time but at the end of script I am getting "closing unused connection" warning. I am not able to understand how to solve this. I want to read a line from socket and then use read.table/scan on that line but it looks like I am opening multiple connections instead of just one. I think I am doing something wrong or not able to understand correct method of doing this. ---------------------------code------------------------ # Create a socket from which to read lines - one at a time (record) reader.socket <- socketConnection( host = 'localhost', 5000, server = TRUE, blocking = TRUE, open = "r", encoding getOption("encoding") ); # now read each record and split/validate it using read.table repeat { # here for each line I am opening new connection! how to avoid it? line.raw <- textConnection(readLines( reader.socket, n = 1, ok = TRUE)); line.raw <- read.table(line.raw, sep=","); if ( length(line.raw) < 1) break; print (showConnections()); print(warnings()); }
Try explicitly closing it: close(reader.socket) or closeAllConnections() On Sat, May 16, 2009 at 8:34 AM, Aval Sarri <aval.sarri at gmail.com> wrote:> Hello; > I am new to R and trying to read a line from socket connection at a > time but at the end of script I am getting "closing unused connection" > warning. I am not able to understand how to solve this. I want to read > a line from socket and then use read.table/scan on that line but it > looks like I am opening multiple connections instead of just one. ?I > think I am doing something wrong or not able to understand correct > method of doing this. > > ---------------------------code------------------------ > # Create a socket from which to read lines - one at a time (record) > reader.socket <- ? socketConnection( host = 'localhost', 5000, > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? server = TRUE, blocking = TRUE, > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? open = "r", encoding > getOption("encoding") ); > # now read each record and split/validate it using read.table > repeat { > ?# here for each line I am opening new connection! how to avoid it? > ?line.raw <- textConnection(readLines( reader.socket, n = 1, ok = TRUE)); > ?line.raw <- read.table(line.raw, sep=","); > > ?if ( length(line.raw) < ?1) > ? ?break; > > ?print (showConnections()); > ?print(warnings()); > } > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
On Sat, May 16, 2009 at 8:34 AM, Aval Sarri <aval.sarri at gmail.com> wrote:> # Create a socket from which to read lines - one at a time (record) > reader.socket <- ? socketConnection( host = 'localhost', 5000, > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? server = TRUE, blocking = TRUE, > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? open = "r", encoding = getOption("encoding") ); > # now read each record and split/validate it using read.table > repeat { > ?# here for each line I am opening new connection! how to avoid it? > ?line.raw <- textConnection(readLines( reader.socket, n = 1, ok = TRUE));What is the function of textConnection here? Is read.table incompatible with socketConnection for some reason?> ?line.raw <- read.table(line.raw, sep=",");> ...at the end of script I am getting "closing unused connection" warning....This is not a problem in itself. For some reason, R gives a warning when connections are garbage collected. Of course, that can be a symptom of poor connection management, but not necessarily. In the present case, you are creating many unnecessary textConnections, and R correctly garbage collects them. -s
Reasonably Related Threads
- read.csv : double quoted numbers
- cannot destroy connection (?) created by readLines in a tryCatch
- cannot destroy connection (?) created by readLines in a tryCatch
- increasing the # of socket connections
- cannot destroy connection (?) created by readLines in a tryCatch