[Rd] help with sockets
Prof Brian Ripley
ripley@stats.ox.ac.uk
Fri, 29 Dec 2000 09:25:03 +0000 (GMT)
On Fri, 29 Dec 2000, Erich Neuwirth wrote:
> i would like to use sockets to accept commands over the net.
> so a socket should be a replacement for stdin.
> whenever i have a complete line, i want to hand it over to the parser.
>
> i cannot do it simply with eval because eval expects a complete
> expression.
>
> additionally, eval and evalq
> seem not to accept strings.
>
> is there an easy way to do this?
>
> let me rephrase
>
> my program should run a loop reading a socket,
> and whenever an and of line arrives,
> the string should be handled like if it had been typed on the command
> line.
>
> can this be done easily?
Not yet, I think. This is the aim of connections (new in 1.2.0), but
socket connections are not yet fully implemented (and nothing is in the
R sources yet). I bet you want to do this on Windows too!
First off, you need to parse, not evaluate the string: eval is called on a
parse tree. So what you will be able to do with a socket connection
is to call eval(parse(file=conname, n = 1)) and that will read one
expression from the connection conname and then evaluate it. So you
could run that in a loop.
I think you can fake this now. When each line arrives, add it to a
character vector `buffer'. Then try parse(n = 1, text=buffer),
and if that succeeds evaluate the result and reinitialize `buffer'
to be empty.
A few details: giving parse() an incomplete expression will get a
parse error, so the call to parse has to be done inside try().
I don't know how to distinguish between an incomplete expression and a
parse error, so this strategy will only work if you always send correct
expressions. (I met exactly the same problem using DDE connections to
S-PLUS for Windows.) Perhaps we can make parse give different error
messages?
An alternative is to use OS facilities to do this. That is, to write a
small program `getfromsocket' that accepts input from the socket and
writes line by line to stdout. Then just use
getfromsocket args | R (or Rterm on Windows).
--
Brian D. Ripley, ripley@stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272860 (secr)
Oxford OX1 3TG, UK Fax: +44 1865 272595
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._