process_socket_server {svSocket} | R Documentation |
The function that processes a command coming from the socket
Description
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.
Usage
process_socket_server(msg, socket, server_port, ...)
processSocket(msg, socket, server_port, ...)
Arguments
msg |
the message send by the client, to be processed. |
socket |
the client socket identifier, as in |
server_port |
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 |
Details
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>>>
tells 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 <<<h>>>
or <<<H>>>
configures 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 scene 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
configure 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 svMisc::parse_text()
and svMisc::capture_all()
in order to
evaluate R code in character string almost exactly the same way as if it was
typed at the command line of a R console.
Value
The results of processing msg
in a character string vector.
See Also
start_socket_server()
, send_socket_clients()
,
par_socket_server()
, svMisc::parse_text()
, svMisc::capture_all()
Examples
## Not run:
# A simple REPL (R eval/process loop) using basic features of processSocket()
repl <- function() {
pars <- par_socket_server("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(process_socket_server(entry, "repl", "")) # Process the entry
}
}
repl()
## End(Not run)