[R] socket server, textConnection and readLines

Krishna Dagli krishna.dagli at gmail.com
Tue Dec 16 17:10:48 CET 2008


Hello;

This is bit long email but hope someone can guide me.

I have questions regarding socket, readLines and textConnection. I am
not sure if my code is efficient (due to textConnection) and how to
handle client disconnect and restart of the socket server in R.

I have a huge(3.5+G) text file on machine 'A', which I want to process
on machine 'B' using read.table (one line or a chunk at a time). On
machine B, I would like to use NWS and multiple R scripts to process
each line/chunk.

To do this I am running netcat (http://netcat.sourceforge.net/) on
macine 'A' and sending data to machine 'B's R socket server.

Here is the data that I have on machine 'A'

---data---
RELIANCE,1200.00,03-NOV-2008,09:00:02:286
RELIANCE,1200.20,03-NOV-2008,09:00:02:287
RELIANCE,1200.10,03-NOV-2008,09:00:02:289
RELIANCE,1201.10,03-NOV-2008,09:00:02:310
INFOSYSTCH,1400.00,03-NOV-2008,09:00:02:286
INFOSYSTCH,1400.20,03-NOV-2008,09:00:02:287
INFOSYSTCH,1400.10,03-NOV-2008,09:00:02:289
INFOSYSTCH,1401.10,03-NOV-2008,09:00:02:310
---end data---


Here is the code that I am using for reading this data on machine 'B'.

---code---
a.connection <- socketConnection(host = 'localhost', 1234,
                                 server = TRUE,
                                 blocking = TRUE,
                                 open = "r",
                                 encoding = getOption("encoding")
                                 )
while(1) {
  line.raw <- NULL;
  line.raw <- readLines( a.connection, n = 1, ok = TRUE);
  tConnection <- textConnection(line.raw);
  line.data <- read.table(tConnection);
  if ( (class(line.data) == 'try-error') ||
     (length(line.data) <= 0)) {
    print ("may be client is disconnected! ");
    break;
  }
  # validate line.data and store it using
  print (line.data);
  close(tConnection);
}
---end code---

Questions:

1) Is there a way to avoid creation and closing of textConnection in
   above code? How can I directly read a line over socket in R?
   If I do not explicitly close the connection I get an warning message
   saying  "closing unused connection 7 (line.raw)".

2) What is the best way to detect that client is disconnected?

3) In C, we can create a socket, bind it but do accept() in side a
   while loop using select() call but how do I do the same in R.


Thanks again for reading such a long email and thanks in advance for
your pointers.

Thanks and Regards
Krishna



More information about the R-help mailing list