Description Usage Arguments Value Note Author(s) See Also Examples
This is the default R function called each time data is send by a client through a socket. It is possible to customize this function and to use customized versions for particular R socket servers.
1 | processSocket(msg, socket, serverport, ...)
|
msg |
the message send by the client, to be processed. |
socket |
the client socket identifier, as in |
serverport |
the port on which the server is running, this is passed by the calling function and can be used internally. |
... |
anything you want to pass to |
The results of processing msg
in a character string vector.
There are special code that one can send to R to easily turn the server
(possibly temporarily) into a given configuration. First, if you want to
persistently store parameters for your client in the R server and make sure
you retrieve the same parameters the next time you reconnect, you should
specify your own identifier. This is done by sending <<<id=myID>>>
at
the very beginning of each of your commands. Always remember that, if you do
not specify an identifier, the name of your socket will be used. Since socket
names can be reused, you should always reinitialize the configuration of your
server the first time you connect to it.
Then, sending <<<esc>>>
breaks current multiline code submission and
flushes the multiline buffer.
The sequence <<<q>>>
at the beginning of a command indicates that the
server wants to disconnect once the command is fully treated by R. Similarly,
the sequence <<<Q>>>
tels the server to disconnect the client before
processing the command (no error message is returned to the client!).
It is easy to turn the server to evaluate R code (including multiline code)
and return the result and disconnect by using the <<<e>>>
sequence at
the beginning of a command. Using the <<<h>>>
or <<<H>>>
configure that server to process a (single-line code only) command silently
and disconnect before (uppercase H) or after (lowercase h) processing that
command. It is the less intrusive mode that is very useful for all commands
that should be executed behind the sceene between R and a R editor or IDE,
like contextual help, calltips, completion lists, etc.). Note that using
these modes in a server that is, otherwise, configured as a multi-line server
does not break current multi-line buffer.
The other sequences that can be used are: <<<s>>>
for a placeholder to
configurate the current server (with configuration parameters after it), and
<<<n>>>
to indicate a newline in your code (submitting two lines of
code as a single one; also works with servers configured as single-line
evaluators).
To debug the R socket server and inspect how commands send by a client are
interpreted by this function, use options(debug.Socket = TRUE)
. This
function uses Parse()
and captureAll()
in order to evaluate
R code in character string almost exactly the same way as if it was
introduced at the command line of a R console.
Philippe Grosjean (phgrosjean@sciviews.org)
startSocketServer
, sendSocketClients
,
parSocket
, parseText
,
captureAll
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | ## Not run:
## A simple REPL (R eval/process loop) using basic features of processSocket()
repl <- function ()
{
pars <- parSocket("repl", "", bare = FALSE) # Parameterize the loop
cat("Enter R code, hit <CTRL-C> or <ESC> to exit\n> ") # First prompt
repeat {
entry <- readLines(n = 1) # Read a line of entry
if (entry == "") entry <- "<<<esc>>>" # Exit from multiline mode
cat(processSocket(entry, "repl", "")) # Process the entry
}
}
repl()
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.