slServer {shinylight} | R Documentation |
Start a ShinyLight server
Description
Start a ShinyLight server
Usage
slServer(
interface,
appDir = NULL,
host = "127.0.0.1",
port = NULL,
daemonize = FALSE,
initialize = NULL
)
Arguments
interface |
List of functions you want to be able to call from
the browser. If you want to use the Shinylight Framework, this should
have one member |
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. For
non-framework apps, the index.html must contain a line containing
|
Value
server object, unless daemonize is TRUE in which case the function will not return.
See Also
slStop
to stop a running server, and
slRunRServer
to run a server that just accepts R code.
Examples
# You can leave out port and daemonize to launch a browser
# pointing at your server
server <- slServer(
port = 50052,
interface = list(
multiply = function(x, y) { x * y }
)
)
# 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:50052/x")
resultdata <- NULL
ws$onMessage(function(event) {
resultdata <<- jsonlite::fromJSON(event$data)$result$data
})
ws$onOpen(function(event) {
ws$send('{ "method": "multiply", "params": { "x": 3, "y": 47 } }')
})
timeout = 30
while(is.null(resultdata) && 0 < timeout) {
later::run_now()
Sys.sleep(0.1)
timeout <- timeout - 1
}
ws$close()
slStop(server)
stopifnot(resultdata == 141) # multiply(3, 47) == 141
grDevices::png() # workaround; you do not have to do this