slRunRServer {shinylight} | R Documentation |
Start a ShinyLight server which runs R that it is sent
Description
Start a ShinyLight server which runs R that it is sent
Usage
slRunRServer(
permittedSymbols,
appDir = NULL,
host = "127.0.0.1",
port = NULL,
daemonize = FALSE,
initialize = NULL
)
Arguments
permittedSymbols |
List of symbols that are permitted in the R
commands passed. Remember to include |
appDir |
Directory containing files to serve (for example system.file("www", package = "your-package")) |
host |
IP address to listen on, default is |
port |
Internet port of the virtual server. If not defined, a random free port will be chosen and the browser will be opened to show the GUI. |
daemonize |
If TRUE, keep serving forever without returning.
This is useful when called from |
initialize |
A json string or list (that will be converted to a
JSON string) to be passed to the JavaScript as initial data. The
index.html must contain a line containing
|
Value
server object, unless daemonize is TRUE.
See Also
slServer
for the more general form of this
function, or slStop
to stop a running server.
shinylight.runR
is the JavaScript function you need
to call to pass R code from the browser to the server.
Examples
server <- slRunRServer(
permitted = list("*"),
port = 50053
)
# Normally we would use shinylight.js to send the function over
# and receive the result, not R and websocket.
ws <- websocket::WebSocket$new("ws://127.0.0.1:50053/x")
resultdata <- NULL
ws$onMessage(function(event) {
resultdata <<- jsonlite::fromJSON(event$data)$result$data
})
ws$onOpen(function(event) {
ws$send('{"method":"runR","params":{"Rcommand":"3 * 57"}}')
})
timeout = 30
while(is.null(resultdata) && 0 < timeout) {
later::run_now()
Sys.sleep(0.1)
timeout <- timeout - 1
}
ws$close()
slStop(server)
stopifnot(resultdata == 171) # 3 * 57 == 171
grDevices::png() # workaround; you do not have to do this