| websocket | R Documentation |
This combines the *-Server family of functions in 'httpuv' with the transpilation functionality provided by 'sketch'.
appA list of functions that define the application.
serverA server handle to be used by 'stopServer'.
logA character vector that keep tracks of all the commands sent to the browser session.
wsA WebSocket channel to handle the communication between the R session and the browser session.
in_handlerA function to handle instructions sent by the browser session.
out_handlerA function to handle instruction sent to the browser session.
envAn environment to store variables temporarily.
portAn integer; the TCP port number.
messageTRUE or FALSE; whether to display a prompt when a server is started and when it is stopped.
connectedTRUE or FALSE; whether a connection has been established. One should ways start the WebSocket server before visiting the web page that connects to the server.
startedTRUE or FALSE; whether a server has been started. Use
the startServer method to start a server.
startServer()Start a WebSocket server
websocket$startServer()
stopServer()Stop a WebSocket server
websocket$stopServer()
listServers()List all running WebSocket servers
websocket$listServers()
stopAllServers()Stop all running WebSocket servers
websocket$stopAllServers()
sketch_mode()Enter sketch mode, in which all commands go through the transpiler before reaching the browser session.
websocket$sketch_mode()
new_app()Create a blank HTML page with interactive access. This function is designed for newcomers.
websocket$new_app( preamble = list(library = c(), script = c(), data = c()), ... )
preamble(Optional) A named list; the preamble to include.
Use the name 'lib' for arguments to load_library, 'script'
for arguments to load_script and 'data' for arguments to
load_data. Note that the "dom" and "websocket" modules are
required and loaded by default.
...Extra parameters to pass to source_r.
The (invisible) temporary file path to the app.
new()Initialise a WebSocket connection
websocket$new(in_handler, out_handler, message = TRUE, port = 9454)
in_handlerA function to handle incoming message, default to be print which only displays the message without any processing.
out_handlerA function to handle outgoing message, default to be compile_exprs which transpiles R commands into JavaScript commands.
messageTRUE or FALSE; whether to display a prompt when a server is started and when it is stopped.
portAn integer; the TCP port number.
A 'websocket' object.
\dontrun{
# Launch a WebSocket server
ws <- websocket$new()
ws$startServer()
ws$listServers() # Check that a server is running
# Launch a 'sketch' application with WebSocket functionality
file <- system.file("test_files/test_websocket.R", package = "sketch")
source_r(file, debug = TRUE) # Launch the default browser
# Enter sketch mode to send commands to the application
ws$sketch_mode()
# Within sketch mode
print("1234")
x <- 10
print(x + 1)
q()
# Back to normal mode, inspect the log and stop the server
ws$log
ws$stopServer()
ws$listServers() # Confirm no server is running
}
clone()The objects of this class are cloneable with this method.
websocket$clone(deep = FALSE)
deepWhether to make a deep clone.
## ------------------------------------------------
## Method `websocket$new`
## ------------------------------------------------
## Not run:
# Launch a WebSocket server
ws <- websocket$new()
ws$startServer()
ws$listServers() # Check that a server is running
# Launch a 'sketch' application with WebSocket functionality
file <- system.file("test_files/test_websocket.R", package = "sketch")
source_r(file, debug = TRUE) # Launch the default browser
# Enter sketch mode to send commands to the application
ws$sketch_mode()
# Within sketch mode
print("1234")
x <- 10
print(x + 1)
q()
# Back to normal mode, inspect the log and stop the server
ws$log
ws$stopServer()
ws$listServers() # Confirm no server is running
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.