sendCommand: Send a command to a web page

View source: R/jrc.R

sendCommandR Documentation

Send a command to a web page

Description

sendCommand sends JavaScript code through the selected WebSocket connection and evaluates it on the specified web page. Use JavaScript function jrc.sendCommand to send R code from the web page and evaluate it in the current R session. All commands send to R from the server will be evaluated only after authorization in the currently running R session (see authorize).

Usage

sendCommand(command, sessionId = NULL, wait = 0)

Arguments

command

A line (or several lines separated by \n) of JavaScript code. This code will be directly evaluated on the web page. No R-side syntax check is performed.

sessionId

An ID of the session to which the command should be sent. Can also be a vector of multiple session IDs. If NULL, the command will be sent to all currently active sessions.

wait

If wait > 0, after sending the message, R will wait for a reply for a given number of seconds. For this time (or until the reply is received), execution of other commands will be halted. Any incoming message from the session will be considered as a reply.

Details

Each opened page gets its own environment, where all the commands are evaluated. Any changes made with the usual assignment operator <- will be limited to this page-specific environment. The changes are still saved, but can be accessed only with getSessionVariable function. To make changes outside of the page-specific environment use <<- instead.

In JavaScript one should use windows.varibleName = "SomeValue" instead of varibleName = "SomeValue", in order to make the variable accessible outside of the current sendCommand call.

This function is a wrapper around sendCommand method of class Session.

See Also

authorize, sendData, sendHTML, callFunction, openPage, getSessionIds.

Examples

 
## Not run: 
# to run this example an installed web browser is required
k <- 0
openPage()
sendCommand(paste0("button = document.createElement('input');",
              "button.type = 'button';",
              "button.addEventListener('click', function() {jrc.sendCommand('k <<- k + 1')});", 
              "button.value = '+1';",
              "document.body.appendChild(button);", collapse = "\n"))
closePage()
## End(Not run)


jrc documentation built on Aug. 23, 2023, 5:10 p.m.

Related to sendCommand in jrc...