jrc/ 0000755 0001762 0000144 00000000000 13617246222 011035 5 ustar ligges users jrc/NAMESPACE 0000644 0001762 0000144 00000001436 13607370714 012263 0 ustar ligges users # Generated by roxygen2: do not edit by hand
export(App)
export(Session)
export(allowFunctions)
export(allowVariables)
export(authorize)
export(callFunction)
export(closePage)
export(closeSession)
export(getMessageIds)
export(getPage)
export(getSession)
export(getSessionIds)
export(getSessionVariable)
export(limitStorage)
export(openPage)
export(removeMessage)
export(removeSessionVariables)
export(sendCommand)
export(sendData)
export(sendHTML)
export(setEnvironment)
export(setSessionVariables)
import(R6)
import(httpuv)
import(mime)
import(stringr)
importFrom(jsonlite,fromJSON)
importFrom(jsonlite,toJSON)
importFrom(stringi,stri_rand_strings)
importFrom(utils,browseURL)
importFrom(utils,compareVersion)
importFrom(utils,menu)
importFrom(utils,object.size)
importFrom(utils,packageVersion)
jrc/man/ 0000755 0001762 0000144 00000000000 13572452705 011615 5 ustar ligges users jrc/man/getSession.Rd 0000644 0001762 0000144 00000000757 13572452661 014241 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/jrc.R
\name{getSession}
\alias{getSession}
\title{Get a session}
\usage{
getSession(sessionId = NULL)
}
\arguments{
\item{sessionId}{ID of the session. If there is only one active session, this argument becomes optional.}
}
\value{
Object of class \code{\link{Session}}.
}
\description{
Returns \code{\link{Session}} by its ID. This function is a wrapper around method
\code{getSession} of class \code{\link{App}}.
}
jrc/man/Session.Rd 0000644 0001762 0000144 00000011103 13572452705 013523 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/jrc.R
\name{Session}
\alias{Session}
\title{Session class}
\description{
Objects of this class handle all the incoming and outgoing messages for one active connection.
Please, avoid creating instances of this class manually. Each \code{Session} object is created, when
a websocket is opened, and serves as a wrapper around it. Manually created object will not have
a websocket connection and thus are not functional.
All sessions are stored within an object of class \code{\link{App}} and cannot exist and function without it.
One can manipulate a session directly, using its methods that are described bellow, via methods of the corresponding
\code{\link{App}} object, or using provided wrapper function (links to them can be found in the Methods section).
}
\section{Fields}{
\describe{
\item{\code{id}}{
Automatically generated ID for this session. ID is a random combination of 6 letters or numbers.
Please, do not change the value of this field.
}
\item{\code{lastActive}}{
Time of the last received message from the session's websocket. The time stamp is generated by
\code{\link[base]{Sys.time}} function.
}
\item{\code{startTime}}{
Time when this session has been started (generated by \code{\link[base]{Sys.time}} function).
}
\item{\code{maxN}}{
Maximum number of messages that can be simultaneously stored for this session. Must be a single number.
If \code{maxN = 0}, any message that requires authorization will be immediately discarded.
If message storage is full, the oldest stored message will be removed, when a new one comes in. Default
value is \code{Inf}.
}
\item{\code{maxSize}}{
Maximum allowed size of the message storage in bytes. Must be a single number.
If \code{maxSize = 0}, any message that requires authorization will be immediately discarded. If message storage is full,
older messages will be removed, until either the allowed storage size is reached, or only one message remains. Default
value is \code{Inf}.
}
}
}
\section{Methods}{
\describe{
\item{\code{getMessageIds()}}{
Returns IDs of all currently stored messages. ID is combination of 6 random letters and numbers,
which is generated, when the message is stored. See also \code{\link{getMessageIds}}.
}
\item{\code{authorize(messageId = NULL, show = FALSE)}}{
Authorizes evaluation of the message. Check \code{\link{authorize}} for more information.
}
\item{\code{removeMessage(messageId = NULL)}}{
Removes a stored message. This can also be done with \code{\link{authorize}} function (set
\code{show = TRUE} and then select to ignore message). See also \code{\link{removeMessage}}.
}
\item{\code{sendCommand(command, wait = 0)}}{
Sends a JavaScript command to be evaluated on the web page. Check
\code{\link{sendCommand}} for more information.
}
\item{\code{callFunction(name, arguments = NULL, assignTo = NULL, wait = 0, thisArg = NULL, ...)}}{
Calls an existing JavaScript
function on the web page. Check \code{\link{callFunction}} for more information.
}
\item{\code{sendData(variableName, variable, wait = 0, keepAsVector = FALSE, rowwise = TRUE)}}{
Sends data and assigns it to
a variable on the web page. Check \code{\link{sendData}} for more information.
}
\item{\code{sendHTML(html, wait = 0)}}{
Sends HTML code that will be appended to the web page. Check \code{\link{sendHTML}} for
more information.
}
\item{\code{sessionVariables(vars = NULL, varName = NULL, remove = NULL)}}{
Sets or returns variables that are used (read or modified) only by this session. If both arguments are
\code{NULL}, returns environment for this session. If \code{vars} is a named list, adds this variables to the
session environment. If \code{varName} is a character, returns a variable with this name how it is seen from
the session. If the variable doesn't exist, throws an error. If \code{remove} is a vector of characters, removes
variables with these names from the session environment. One can add variables to the session environment,
get one back and remove variables with a single function call. Check \code{\link{setSessionVariables}},
\code{\link{getSessionVariable}}, \code{\link{removeSessionVariables}} for more information.
}
}
Note, that \code{Session} class has some other public methods that are not mentioned in this list. These methods are
intended to be used only by other functions of \code{jrc} package and therefore are not documented.
}
jrc/man/closePage.Rd 0000644 0001762 0000144 00000000551 13572452661 014010 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/jrc.R
\name{closePage}
\alias{closePage}
\title{Stop server}
\usage{
closePage()
}
\description{
Stops the server and closes all currently opened pages (if any). This function is a
wrapper of \code{stopServer} method of class \code{\link{App}}.
}
\seealso{
\code{\link{openPage}}
}
jrc/man/removeMessage.Rd 0000644 0001762 0000144 00000001311 13572452661 014703 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/jrc.R
\name{removeMessage}
\alias{removeMessage}
\title{Removes a stored message}
\usage{
removeMessage(sessionId = NULL, messageId = NULL)
}
\arguments{
\item{sessionId}{ID of the session from where to remove a message. If there is only one active session, this argument
becomes optional.}
\item{messageId}{ID of the message to remove. If there is only one stored message, this argument becomes optional.}
}
\description{
Removes a message from the storage of a session. This function is a wrapper around
method \code{removeMessage} of class \code{\link{Session}}.
}
\seealso{
\code{\link{authorize}}, \code{\link{getMessageIds}}.
}
jrc/man/allowFunctions.Rd 0000644 0001762 0000144 00000001553 13572452661 015120 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/jrc.R
\name{allowFunctions}
\alias{allowFunctions}
\title{Allow function calls without authorization}
\usage{
allowFunctions(funs = NULL)
}
\arguments{
\item{funs}{Vector of function names to be added to the list. If \code{NULL},
returns names of all currently allowed R functions.}
}
\value{
Names of all currently allowed functions if \code{funs = NULL}.
}
\description{
Adds R function names to the list of functions, that
can be called from a web page without manual confirmation on the R side.
}
\examples{
\donttest{openPage()
allowFunctions(c("myFunction1", "print", "someObject$method"))
funs <- allowFunctions()
closePage()}
}
\seealso{
\code{\link{allowVariables}}, \code{\link{authorize}}, \code{\link{openPage}} (check argument
\code{allowedFunctions}), \code{\link{callFunction}}.
}
jrc/man/setSessionVariables.Rd 0000644 0001762 0000144 00000003714 13572452661 016102 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/jrc.R
\name{setSessionVariables}
\alias{setSessionVariables}
\title{Adds variables to a session environment}
\usage{
setSessionVariables(vars, sessionId = NULL, makeDefault = FALSE)
}
\arguments{
\item{vars}{Named list of variables to be added to a session environment. Names are required and
will be used as variable names.}
\item{sessionId}{ID of the session to which variables should be added. Can also be a vector of
multiple session IDs. If \code{NULL}, then variables will be added to all currently active sessions.}
\item{makeDefault}{If \code{TRUE} then, in addition, the specified variables will be added to each
new opened session as default ones.}
}
\description{
Each client session in \code{jrc}, gets its own environment that can be accessed only by this
session (or from the outside with the \code{\link{getSessionVariable}} function). General purpose
of these environments is to store some session-specific information such as state of the app for
each user. It can also be used to mask variables from the user: if there are two variables with the
same name in the session environment and outside of it, user will not be able to see the latter one.
This function adds new variables to a session environment or changes values of some existing ones.
}
\details{
This function is a wrapper around method \code{sessionVariables} of class \code{\link{Session}}.
If \code{makeDefault = TRUE}, it is also a wrapper around method \code{sessionVariables} of class
\code{\link{App}}. The first one changes the current state of the session environment, while the
second specifies default variables for each new session.
}
\examples{
\donttest{openPage(allowedFunctions = "f", allowedVariables = "res")
m <- 1
f <- function() {v * m}
setSessionVariables(list(v = 1:10, m = 2))
sendCommand("jrc.callFunction('f', [], 'res')", wait = 1)
print(res)
closePage()}
}
\seealso{
\code{\link{getSessionVariable}}.
}
jrc/man/sendHTML.Rd 0000644 0001762 0000144 00000002335 13572452661 013526 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/jrc.R
\name{sendHTML}
\alias{sendHTML}
\title{Send HTML to a web page}
\usage{
sendHTML(html = "", sessionId = NULL, wait = 0)
}
\arguments{
\item{html}{HTML code that will be added to the web page.}
\item{sessionId}{An ID of the session to which the HTML should be sent. Can also be a vector of multiple session IDs.
If \code{NULL}, the HTML will be sent to all currently active sessions.}
\item{wait}{If \code{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.}
}
\description{
Sends a piece of HTML code to a web page and adds it at the end
or the \code{body} element. This function is a wrapper around \code{sendHTML} method of
class \code{\link{Session}}.
}
\examples{
\donttest{
openPage(FALSE)
sendHTML("Test...")
sendHTML("This is bold")
sendHTML("
")}
}
\seealso{
\code{\link{sendData}}, \code{\link{sendCommand}}, \code{\link{callFunction}},
\code{\link{openPage}}.
}
jrc/man/getMessageIds.Rd 0000644 0001762 0000144 00000002242 13572452661 014631 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/jrc.R
\name{getMessageIds}
\alias{getMessageIds}
\title{Get IDs of all stored messages}
\usage{
getMessageIds(sessionId = NULL, simplify = TRUE)
}
\arguments{
\item{sessionId}{ID of the session for which to return message IDs. Can also be a vector of multiple session IDs.
If \code{NULL}, returns message IDs for all currently active sessions.}
\item{simplify}{If \code{TRUE} and only one session ID is provided (or there is only one active session), returns
a vector of message IDs. Otherwise returns a named list with one vector for each requested session.}
}
\value{
Either a named list or a vector with message IDs.
}
\description{
Returns IDs of all currently stored messages.
}
\details{
For security reasons, most of the messages that are received
from web pages require manual authorization in the R session with \code{\link{authorize}} function. Until that happens,
messages are given randomly generated IDs and are stored in memory.
This function is a wrapper around method \code{getMessageIds} of class \code{\link{Session}}.
}
\seealso{
\code{\link{authorize}}, \code{\link{getSessionIds}}.
}
jrc/man/App.Rd 0000644 0001762 0000144 00000011231 13572452661 012623 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/jrc.R
\name{App}
\alias{App}
\title{App class}
\description{
Object of this class represents the entire jrc-based app. It stores all the active connections,
client-specific variables and all the global app settings.
You can create interactive apps by initializing
new instances of this class and manage the apps with the methods that are described below. There are no limitations
on the number of apps that can run simultaneously in one R session.
A wrapper function is also exported for almost each method (see links in the Methods section). This functions allow
you to gain full control over the app without ever dealing with this class. However, in this case only a single app
can run per R session. Attempt to create a new app (with \code{\link{openPage}} function) will force the existing one (if any)
to stop. You can always get the \code{App} object for the currently running app with \code{\link{getPage}} function.
}
\section{Methods}{
\describe{
\item{\code{new(rootDirectory = NULL, startPage = NULL, onStart = NULL,
connectionNumber = Inf, allowedFunctions = c(), allowedVariables = c(), sessionVars = NULL)}}{
Creates a new instance of class \code{App}. Check \code{\link{openPage}} man page for information about
arguments.
}
\item{\code{startServer(port = NULL)}}{
Starts a local server that listens to a given port. If \code{port = NULL}, picks a random available port.
See also \code{\link{openPage}}.
}
\item{\code{stopServer()}}{
Closes all active sessions and stops a running server. See also \code{\link{closePage}}.
}
\item{\code{openPage(useViewer = TRUE, browser = NULL)}}{
Opens a new web page either in a browser, or in the R Studio viewer. If \code{useViewer = FALSE} and browser is not selected,
a default installed browser is used. If browser is specified, \code{useViewer} is ignored. This method returns
a new \code{\link{Session}} object, which should correspond to the page that has been just opened. However, if someone would start
a new connection at the moment when \code{openPage} method is called, it may return a wrong session. See also \code{\link{openPage}}.
}
\item{\code{getSession(sessionId = NULL)}}{
Returns a session with the given ID or \code{NULL} if session with this ID doesn't exist. If \code{sessionId = NULL}
and there is only one active session, returns it. See also \code{\link{getSession}}.
}
\item{\code{closeSession(sessionId = NULL, inactive = NULL, old = NULL)}}{
Closes websocket connection of one or multiple sessions and removes all the related data from the app. For more information on
the arguments, please, check \code{\link{closeSession}} man page.
}
\item{\code{getSessionIds()}}{
Returns IDs of all currently active sessions. See also \code{\link{getSessionIds}}.
}
\item{\code{setEnvironment(envir)}}{
Specifies the outer environment of the app, in which all the messages from the web pages will be evaluated. For more information,
please, check \code{\link{setEnvironment}}.
}
\item{\code{allowFunctions(funs = NULL)}}{
Adds function names to a list of allowed R functions. These functions can be called from a web page without authorization
on the R side. If \code{funs = NULL}, returns a list of all currently allowed functions. For more information,
please, check \code{\link{allowFunctions}}.
}
\item{\code{allowVariables(vars)}}{
Adds variable names to the list of allowed variables. These variables can be changed from a web page without
authorization on the R side. If \code{vars = NULL}, then returns a vector of names of all currently allowed variables.
For more information, please, check \code{\link{allowVariables}}.
}
\item{\code{startPage(path = NULL)}}{
Sets path to a starting web page of the app. Path can be full, relative to the app's root directory or relative
to the current R working directory. If
\code{path = NULL}, returns current path to the starting page.
}
\item{\code{rootDirectory(path = NULL)}}{
Sets path to the root directory for the server. Any file, requested by the server, will be looked for in this directory.
Can be a full path or a path relative to the current R working directory. If \code{path = NULL}, returns path to the
current root directory.
}
\item{\code{numberOfConnections(maxCon = NULL)}}{
Sets maximum number of connections that can be active simultaneously. If \code{maxCon = NULL}, returns current value of
maximum allowed number of connections. Default value is \code{Inf}.
}
}
}
jrc/man/getSessionVariable.Rd 0000644 0001762 0000144 00000002665 13572452661 015707 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/jrc.R
\name{getSessionVariable}
\alias{getSessionVariable}
\title{Get a variable from a client session environment}
\usage{
getSessionVariable(varName, sessionId = NULL)
}
\arguments{
\item{varName}{Name of the variable to search for. Must be a character.}
\item{sessionId}{ID of the session. If there is only one active session, this argument becomes optional.}
}
\value{
Requested variable
}
\description{
This function returns a variable, how it is seen from a session, e.g. for all the received function calls and
commands. It searches for the variable in the session environment first, and then, if variable is not found, checks enclosing
frames of the environment, starting from the outer environment of the app (see \code{\link{setEnvironment}}). If the variable
doesn't exist, throws an error.
}
\details{
This function
is a wrapper around method \code{sessionVariables} of the class \code{\link{Session}}.
}
\examples{
\donttest{f <- function(x) {x * 3}
openPage(allowedFunctions = "f", allowedVariables = "k")
getPage()$openPage(FALSE)
id1 <- getSessionIds()[1]
id2 <- getSessionIds()[2]
sendCommand("jrc.callFunction('f', [10], 'k')", sessionId = id1, wait = 3)
sendCommand("jrc.callFunction('f', [20], 'k')", sessionId = id2, wait = 3)
k1 <- getSessionVariable("k", id1)
k2 <- getSessionVariable("k", id2)
closePage()}
}
\seealso{
\code{\link{setSessionVariables}}
}
jrc/man/allowVariables.Rd 0000644 0001762 0000144 00000001547 13572452661 015063 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/jrc.R
\name{allowVariables}
\alias{allowVariables}
\title{Allow variable assignment without authorization}
\usage{
allowVariables(vars = NULL)
}
\arguments{
\item{vars}{Vector of variable names to be added to the list. If \code{NULL},
returns names of all currently allowed variables.}
}
\value{
Names of all currently allowed variables if \code{vars = NULL}.
}
\description{
This function adds variable names to the list of variables, that
can be modified from a web page without manual confirmation on the R side.
}
\examples{
\donttest{openPage()
allowVariables(c("myVariable", "anotherOne"))
vars <- allowVariables()
closePage()}
}
\seealso{
\code{\link{allowFunctions}}, \code{\link{authorize}}, \code{\link{openPage}} (check argument
\code{allowedVariables}), \code{\link{sendData}}.
}
jrc/man/authorize.Rd 0000644 0001762 0000144 00000004045 13572452661 014122 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/jrc.R
\name{authorize}
\alias{authorize}
\title{Authorize further message processing}
\usage{
authorize(sessionId = NULL, messageId = NULL, show = FALSE)
}
\arguments{
\item{sessionId}{ID of the session that received the message. If there is only one active session, this
argument becomes optional.}
\item{messageId}{ID of the message to be processed. If the session has only one stored message, this argument becomes
optional.}
\item{show}{If \code{TRUE} information about the message will be shown first. After that user gets
a choice to go on with evaluation, to ignore the message (meaning it will be removed from memory) or to
do nothing.}
}
\description{
\code{jrc} library allows one to get full control over the currently running R session from
a web page. Therefore for security reasons one should manually authorize function calls,
variable assignments or expression evaluations. All the received messages that are not
processed automatically are given an ID and stored. This function allows a message with the
given ID to be evaluated. It can also show a short description of the message and give user
a choice between running it or discarding.
}
\details{
Expressions has to be always authorized before evaluation. One can specify a list of
variables that can be changed automatically and functions that can be called without
authorization.
This function is a wrapper around \code{authorize} method of class \code{\link{Session}}.
}
\examples{
\donttest{openPage()
callFunction("jrc.sendCommand", list("k <<- 10"), wait = 1)
allowVariables("x")
callFunction("jrc.sendData", list("x", 15), wait = 1)
callFunction("jrc.sendData", list("y", 20), wait = 1)
msgId <- getMessageIds()
authorize(messageId = msgId[1])
#run that to first see some information about the message
#authorize(messageId = msgId[2], show = TRUE)
closePage()}
}
\seealso{
\code{\link{allowFunctions}}, \code{\link{allowVariables}}, \code{\link{limitStorage}}, \code{\link{getSessionIds}},
\code{\link{getMessageIds}}.
}
jrc/man/getPage.Rd 0000644 0001762 0000144 00000001504 13572452661 013461 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/jrc.R
\name{getPage}
\alias{getPage}
\title{Get the currently running app}
\usage{
getPage()
}
\value{
Object of class \code{\link{App}} or \code{NULL} if there is no active app.
}
\description{
\code{jrc} offers two ways to control an interactive app. One is by using methods of classes
\code{\link{App}} and \code{\link{Session}}. This allows one to have any number of apps within one
R session, but requires some understanding of object oriented programming. Another way is to use
provided wrapper functions that are exported by the package. These functions internally work with
the \code{\link{App}} object, which is stored in the package namespace upon initialization with
\code{\link{openPage}} function. \code{getPage} returns this object if any.
}
jrc/man/openPage.Rd 0000644 0001762 0000144 00000011656 13607370714 013651 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/jrc.R
\name{openPage}
\alias{openPage}
\title{Create a server}
\usage{
openPage(
useViewer = TRUE,
rootDirectory = NULL,
startPage = NULL,
port = NULL,
browser = NULL,
allowedFunctions = NULL,
allowedVariables = NULL,
connectionNumber = Inf,
sessionVars = NULL,
onStart = NULL
)
}
\arguments{
\item{useViewer}{If \code{TRUE}, the new web page will be opened in the RStudio Viewer. If \code{FALSE},
a default web browser will be used (if other is not specified with the \code{browser} argument).}
\item{rootDirectory}{A path to the root directory fpr the server. Any file, requested by the server
will be searched for in this directory. If \code{rootDirectory} is not
defined, the \code{http_root} in the package directory will be used as a root directory.}
\item{startPage}{A path to an HTML file that should be used as a starting page of the app.
It can be an absolute path to a local file, or it can be relative to the \code{rootDirectory}
or to the current R working directory. If \code{startPage} is not defined, an empty page will be used.
The file must have \emph{.html} extension.}
\item{port}{Defines which TCP port the server will listen to. If not defined, random available port
will be used (see \code{\link[httpuv]{randomPort}}).}
\item{browser}{A browser in which to open a new web page.
If not defined, default browser will be used. For more information check \code{\link[utils]{browseURL}}.
If this argument is specified, \code{useViewer} will be ignored.}
\item{allowedFunctions}{List of functions that can be called from a web page without any additional actions
on the R side. All other functions will require authorization in the current R session before they are called.
This argument should be a vector of R function names. Check \code{\link{authorize}} and \code{\link{allowFunctions}}
for more information.}
\item{allowedVariables}{List of variables that can be modified from a web page without any additional actions
on the R side. All other variable reassignments must be confirmed in the current R session.
This argument should be a vector of variable names. Check \code{\link{authorize}} and \code{\link{allowVariables}}
for more information.}
\item{connectionNumber}{Maximum number of connections that is allowed to be active simultaneously.}
\item{sessionVars}{Named list of variables, that will be declared for each session, when a new connection is opened.
Any changes to these variables will affect only a certain session. Thus they can be used, for instance, to
store a state of each session. For more information, please, check \code{\link{setSessionVariables}}.}
\item{onStart}{A callback function that will be executed when a new connection is opened. This function gets a single
argument, which is an object of class \code{\link{Session}}. General purpose of the function is to populate each
new web page with some default content.}
}
\value{
Object of class \code{\link{App}}.
}
\description{
\code{openPage} starts a server and opens a new page with a websocket connection between it and the current
R session. After that, messages can be exchanged between R session and the web page to generate content on the
web page and to trigger calculations in R as a response to user activity on the page.
}
\details{
\code{jrc} supports four types of messages:
\itemize{
\item{Commands are pieces of R or JavaScript code that will be evaluated on the receiving side. Note,
that any command from a web page must be authorized in the R session for security reasons. A message
with information about how to do that is printed in the console each time a command is received. For more
information, please, check \code{\link{sendCommand}}.}
\item{Data is any variable that is sent to or from the R session. It must always come with a
name of the variable to which it should be assigned on the receiving side. For more information, please,
check \code{\link{sendData}}.}
\item{Function calls can be triggered on each side of the websocket connection. Alongside the function name,
one can also send a list of arguments and name of a variable to which the returned value of the function will
be assigned. For more information, please, check \code{\link{callFunction}}.}
\item{Unlike other types of messages, HTML code can be sent only from the R session to a web page. This code will
be added to the body of the page.}
}
\code{openPage} function is a wrapper around several methods of class \code{\link{App}}. First, it creates an
instance of this class. Then it starts a server that listens to the given port. And finally, it attempts
to open a new web page. It also stores a new app object in the package namespace, which allows other
wrapper functions to access it.
}
\seealso{
\code{\link{closePage}}, \code{\link{setEnvironment}}, \code{\link{limitStorage}}, \code{\link{allowVariables}},
\code{\link{allowFunctions}}, \code{\link{setSessionVariables}}.
}
jrc/man/getSessionIds.Rd 0000644 0001762 0000144 00000000753 13572452661 014675 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/jrc.R
\name{getSessionIds}
\alias{getSessionIds}
\title{Get IDs of all active sessions}
\usage{
getSessionIds()
}
\value{
Vector of session IDs.
}
\description{
Returns IDs of all currently active sessions. An ID is a randomly generated combination of 6 letters and
numbers that is assigned to each session upon opening. This function is a wrapper around method \code{getSessionIds}
of class \code{\link{App}}.
}
jrc/man/removeSessionVariables.Rd 0000644 0001762 0000144 00000002457 13572452705 016606 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/jrc.R
\name{removeSessionVariables}
\alias{removeSessionVariables}
\title{Remove variables from a client session environment}
\usage{
removeSessionVariables(varNames, sessionId = NULL)
}
\arguments{
\item{varNames}{Names of variables to remove.}
\item{sessionId}{ID of the session. If there is only one active session, this argument becomes optional.}
}
\description{
This function removes variables from the environment of a client session. It allows, for instance, to unmask
a variable with the same name from the outer app environment (see \code{\link{setEnvironment}}) for the session
(check the example below). This function
is a wrapper around method \code{sessionVariables} of the class \code{\link{Session}}.
}
\examples{
\donttest{openPage(allowedVariables = "k", sessionVars = list(k = 10))
k <- -1
getPage()$openPage(FALSE)
id1 <- getSessionIds()[1]
id2 <- getSessionIds()[2]
removeSessionVariables("k", id1)
#this changes global 'k', since the variable is no longer masked
sendCommand("jrc.sendData('k', 1)", sessionId = id1, wait = 3)
#this doesn't affect global 'k'
sendCommand("jrc.sendData('k', 5)", sessionId = id2, wait = 3)
local_k <- getSessionVariable("k", id2)
closePage()}
}
\seealso{
\code{\link{setSessionVariables}}
}
jrc/man/sendCommand.Rd 0000644 0001762 0000144 00000004730 13572452661 014341 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/jrc.R
\name{sendCommand}
\alias{sendCommand}
\title{Send a command to a web page}
\usage{
sendCommand(command, sessionId = NULL, wait = 0)
}
\arguments{
\item{command}{A line (or several lines separated by \code{\\n}) of JavaScript code. This code
will be directly evaluated on the web page. No R-side syntax check is performed.}
\item{sessionId}{An ID of the session to which the command should be sent. Can also be a vector of multiple session IDs.
If \code{NULL}, the command will be sent to all currently active sessions.}
\item{wait}{If \code{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.}
}
\description{
\code{sendCommand} sends JavaScript code through the selected websocket connection and evaluates it on the specified
web page. Use JavaScript function \code{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 \code{\link{authorize}}).
}
\details{
Each opened page gets its own environment, where all the commands are evaluated. Any changes
made with the usual assignment operator \code{<-} will be limited to this page-specific environment. The changes
are still saved, but can be accessed only with \code{\link{getSessionVariable}} function. To make changes outside
of the page-specific environment use \code{<<-} instead.
In JavaScript one should use \code{windows.varibleName = "SomeValue"}
instead of \code{varibleName = "SomeValue"}, in order to make the variable accessible outside of the
current \code{sendCommand} call.
This function is a wrapper around \code{sendCommand} method of class \code{\link{Session}}.
}
\examples{
\donttest{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()}
}
\seealso{
\code{\link{authorize}}, \code{\link{sendData}}, \code{\link{sendHTML}}, \code{\link{callFunction}},
\code{\link{openPage}}, \code{\link{getSessionIds}}.
}
jrc/man/setEnvironment.Rd 0000644 0001762 0000144 00000002014 13572452661 015122 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/jrc.R
\name{setEnvironment}
\alias{setEnvironment}
\title{Set Environment}
\usage{
setEnvironment(envir)
}
\arguments{
\item{envir}{Environment to be used as outer environment.}
}
\description{
Defines the outer environment of the app. Outer environment is a parent for all session environments.
It is used to store variables that are common for all the client sessions. The only way to make changes outside of
the outer environment is to use the global assignment operator \code{<<-} if and only if changes are
made to the variable that does not exist in the outer environment.
}
\details{
By default, an environment where app was initialized (via \code{\link{openPage}} function or with \code{App$new()} call)
is used.
This function is a wrapper around \code{setEnvironment} method of class \code{\link{App}}.
}
\examples{
\donttest{
openPage()
e <- new.env()
setEnvironment(e)
sendCommand("jrc.sendData('x', 10)", wait = 3)
print(e$x)
closePage()
}
}
jrc/man/limitStorage.Rd 0000644 0001762 0000144 00000003542 13572452661 014554 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/jrc.R
\name{limitStorage}
\alias{limitStorage}
\title{Change size of the message storage}
\usage{
limitStorage(n = NULL, size = NULL, sessionId = NULL)
}
\arguments{
\item{n}{Maximum number of messages that can be stored simultaneously.}
\item{size}{Maximum total size of all stored messages in bytes.}
\item{sessionId}{ID of the session, for which the storage size should be changed.
Can also be a vector of session IDs to change storage size for multiple sessions at once.
If \code{NULL}, changes will be applied to all currently active sessions.}
}
\description{
This function allows to change number or total size of the messages
that are received via the websocket and are stored in memory.
}
\details{
For security reasons, control of the currently running R session is limited
to calling and changing only some user specified functions and variables. All other
messages are stored in memory and can be later processed
by calling \code{\link{authorize}} function. To prevent overuse of memory, one can
limit size of the storage by number of messages or by their total size estimated
by \code{\link[utils]{object.size}}. If the storage grows above these limits, older
messages are removed. The last received message will not be removed even if it
takes more memory than is allowed. If any of the size parameters are
set to zero, no massages will be stored. In this case, any message that requires authorization will be
automatically discarded.
One can also directly change public fields \code{maxN} and \code{maxSize} of any object
of class \code{\link{Session}} (see also \code{\link{getSession}}).
}
\examples{
\donttest{openPage()
limitStorage(n = 10)
limitStorage(size = 10 * 1024^2)
closePage()}
}
\seealso{
\code{\link{authorize}}, \code{\link{allowFunctions}}, \code{\link{allowVariables}}.
}
jrc/man/callFunction.Rd 0000644 0001762 0000144 00000010210 13572452661 014520 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/jrc.R
\name{callFunction}
\alias{callFunction}
\title{Trigger a function call}
\usage{
callFunction(
name,
arguments = NULL,
assignTo = NULL,
wait = 0,
sessionId = NULL,
thisArg = NULL,
...
)
}
\arguments{
\item{name}{Name of the function. If the function is a method of some object
its name must contain the full chain of calls (e.g. \code{myArray.sort} or
\code{Math.rand}).}
\item{arguments}{List of arguments for the function. Note that in JavaScript
arguments must be given in a fixed order, naming is not necessary and will
be ignored.}
\item{assignTo}{Name of a variable to which will be assigned the returned value
of the called function.}
\item{wait}{If \code{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.}
\item{sessionId}{An ID of the session to which the function call should be sent. Can also be a vector of multiple
session IDs. If \code{NULL}, the function call will be sent to all currently active sessions.}
\item{thisArg}{JavaScript functions (methods) can belong to some object, which
is referred to as \code{this} inside the function (e.g. in
\code{someObject.myFunction()} function \code{myFunction} is a method of \code{someObject}).
\code{thisArg} specifies object that will be known as \code{this} inside the function. If \code{NULL},
the function will be applied to the global object (\code{window}).}
\item{...}{further arguments passed to \code{\link{sendData}}. It is used to send
\code{arguments} to the web page.}
}
\description{
Calls a function in a web page by its name. It can also pass a list of arguments for the function and
save the returned result to a variable.
}
\details{
JavaScript counterpart is \code{jrc.callFunction(name, arguments, assignTo, package, internal)}.
Its arguments are:
\describe{
\item{\code{name}}{
Name of an R function. If function name hasn't been previously added to the list
of allowed functions (see \code{\link{allowFunctions}} or \code{allowedFunctions} argument of \code{\link{openPage}}),
attempt to call it from a web page will require manual authorization on the R side.
}
\item{\code{arguments} (optional)}{
arguments for the function. This should be an Array (for unnamed arguments) or an Object with argument names as keys
(for named arguments).
}
\item{\code{assignTo} (optional)}{
Name of the variable to which the returned value of the function will be assigned in the R session.
If the variable name hasn't been previously added to the list
of allowed variables (see \code{\link{allowVariables}} or \code{allowedVariables} argument of \code{\link{openPage}}),
attempt to assign it from a web page will require manual authorization on the R side.
}
\item{\code{package} (optional)}{
If the function needs to be imported from an installed package, name of this package.
}
\item{\code{internal} (optional)}{
Whether assignment of the function returned value should happen internally or not. If \code{true}, result will be stored
in the session environment and can be accessed from the outside with \code{\link{getSessionVariable}}
function. If \code{false}, result will be saved to the outer environment of the app (see \code{\link{setEnvironment}}).
By default, uses \code{true} for variables that already exist in the session environment
(see \code{\link{setSessionVariables}} or \code{sessionVariables} argument of the \code{\link{openPage}} function)
and \code{false} otherwise.
}
}
This function is a wrapper
around \code{callFunction} method of class \code{\link{Session}}.
}
\examples{
\donttest{
openPage()
callFunction("alert", list("Some alertText"))
callFunction("Math.random", assignTo = "randomNumber")
sendCommand("alert(randomNumber)")
}
}
\seealso{
\code{\link{authorize}}, \code{\link{allowFunctions}}, \code{\link{allowVariables}},
\code{\link{setEnvironment}}, \code{\link{getSessionIds}}.
}
jrc/man/sendData.Rd 0000644 0001762 0000144 00000005725 13576164444 013644 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/jrc.R
\name{sendData}
\alias{sendData}
\title{Send data to a web page}
\usage{
sendData(
variableName,
variable,
keepAsVector = FALSE,
rowwise = TRUE,
sessionId = NULL,
wait = 0
)
}
\arguments{
\item{variableName}{Name that the variable will have on the web page.}
\item{variable}{Variable to send.}
\item{keepAsVector}{If \code{TRUE}, variables with length 1 will be saved as arrays on the web page, otherwise they
will be converted to atomic types.}
\item{rowwise}{If \code{TRUE}, matrices and data.frames will be transformed into JavaScript objects or arrays
row wise (e.g. a matrix will become an Array of its rows).}
\item{sessionId}{An ID of the session to which the data should be sent. Can also be a vector of multiple session IDs.
If \code{NULL}, the data will be sent to all currently active sessions.}
\item{wait}{If \code{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.}
}
\description{
Sends a variable to a web page, where it is saved under a specified name. This function
is a wrapper around \code{sendData} method of class \code{\link{Session}}.
}
\details{
To send data back from the web page to the current R session one should use\code{jrc.sendData(variableName, variable, internal)}.
Its arguments are:
\describe{
\item{\code{variableName}}{
Name that the variable will have in the R session. If variable name hasn't been previously added to the list
of allowed variables (see \code{\link{allowVariables}} or \code{allowedVariables} argument of the \code{\link{openPage}}
function), attempt to assign it from a web page will require manual authorization on the R side.
}
\item{\code{variable}}{
Variable to send.
}
\item{\code{internal} (optional)}{
Whether this variable should be used only by the session that sent it. If \code{true}, variable will be stored
in the session-specific environment and can be accessed from the outside with \code{\link{getSessionVariable}}
function. If \code{false}, variable will be saved to the outer environment of the app (see \code{\link{setEnvironment}}).
By default, uses \code{true} for variables that already exist in the session specific environment
(see \code{\link{setSessionVariables}} or \code{sessionVariables} argument of the \code{\link{openPage}} function.)
and \code{false} otherwise.
}
}
}
\examples{
\donttest{openPage()
x <- 1:100
sendData("x", x)
sendCommand("console.log(x);")
sendCommand("jrc.sendData('x', x.filter(function(e) {return e \% 2 == 0}))")
closePage()}
}
\seealso{
\code{\link{authorize}}, \code{\link{allowVariables}}, \code{\link{sendCommand}},
\code{\link{callFunction}}, \code{\link{sendHTML}}, \code{\link{openPage}}, \code{\link{getSessionIds}}.
}
jrc/man/closeSession.Rd 0000644 0001762 0000144 00000002650 13572452661 014561 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/jrc.R
\name{closeSession}
\alias{closeSession}
\title{Close one or several client sessions}
\usage{
closeSession(sessionId = NULL, inactive = NULL, old = NULL)
}
\arguments{
\item{sessionId}{IDs of the sessions to close. Can be a vector of multiple IDs.}
\item{inactive}{All sessions that were inactive (didn't receive any messages) for the
specified amount of time (in seconds) will be closed.}
\item{old}{All sessions that were opened for at least specified amount of time (in seconds)
will be closed.}
}
\description{
Closes websocket connections for the selected client sessions and removes all the related
information from memory. If no arguments are provided and there is only one active session,
closes it. This function is a wrapper around method \code{closeSession} of
class \code{\link{App}}.
}
\examples{
\donttest{start <- Sys.time()
openPage()
app <- getPage()
time <- Sys.time()
app$openPage(FALSE)
app$openPage(FALSE)
print(getSessionIds())
# No sessions will be closed
closeSession(old = Sys.time() - start)
print(getSessionIds())
# One session (the one that has been opened first) will be closed
closeSession(old = Sys.time() - time)
print(getSessionIds())
time <- Sys.time()
sendCommand("jrc.sendCommand('print(\"Hi!\")')", sessionId = getSessionIds()[1], wait = 3)
closeSession(inactive = Sys.time() - time)
closeSession()
closePage()}
}
jrc/DESCRIPTION 0000644 0001762 0000144 00000001700 13617246222 012541 0 ustar ligges users Package: jrc
Type: Package
Title: Exchange Commands Between R and 'JavaScript'
Version: 0.3.1
Date: 2020-02-06
Authors@R: c(
person("Svetlana", "Ovchinnikova", role = c("aut", "cre"), email = "s.ovchinnikova@zmbh.uni-heidelberg.de"),
person("Simon", "Anders", role = c("aut"), email = "sanders@fs.tum.de")
)
Description: An 'httpuv' based bridge between R and 'JavaScript'. Provides an easy way to exchange commands and data between a web page and a currently running R session.
License: GPL-3
Imports: httpuv, jsonlite, utils, stringr, stringi, mime, R6
RoxygenNote: 7.0.1
URL: https://github.com/anders-biostat/jrc
BugReports: https://github.com/anders-biostat/jrc/issues
Suggests: testthat
NeedsCompilation: no
Packaged: 2020-02-07 10:44:52 UTC; tyranchick
Author: Svetlana Ovchinnikova [aut, cre],
Simon Anders [aut]
Maintainer: Svetlana Ovchinnikova
Repository: CRAN
Date/Publication: 2020-02-07 11:40:02 UTC
jrc/R/ 0000755 0001762 0000144 00000000000 13617237644 011246 5 ustar ligges users jrc/R/jrc.R 0000644 0001762 0000144 00000214747 13617025177 012162 0 ustar ligges users #' @name Session
#' @title Session class
#'
#' @description Objects of this class handle all the incoming and outgoing messages for one active connection.
#' Please, avoid creating instances of this class manually. Each \code{Session} object is created, when
#' a websocket is opened, and serves as a wrapper around it. Manually created object will not have
#' a websocket connection and thus are not functional.
#'
#' All sessions are stored within an object of class \code{\link{App}} and cannot exist and function without it.
#' One can manipulate a session directly, using its methods that are described bellow, via methods of the corresponding
#' \code{\link{App}} object, or using provided wrapper function (links to them can be found in the Methods section).
#'
#' @section Fields:
#' \describe{
#' \item{\code{id}}{
#' Automatically generated ID for this session. ID is a random combination of 6 letters or numbers.
#' Please, do not change the value of this field.
#' }
#' \item{\code{lastActive}}{
#' Time of the last received message from the session's websocket. The time stamp is generated by
#' \code{\link[base]{Sys.time}} function.
#' }
#' \item{\code{startTime}}{
#' Time when this session has been started (generated by \code{\link[base]{Sys.time}} function).
#' }
#' \item{\code{maxN}}{
#' Maximum number of messages that can be simultaneously stored for this session. Must be a single number.
#' If \code{maxN = 0}, any message that requires authorization will be immediately discarded.
#' If message storage is full, the oldest stored message will be removed, when a new one comes in. Default
#' value is \code{Inf}.
#' }
#' \item{\code{maxSize}}{
#' Maximum allowed size of the message storage in bytes. Must be a single number.
#' If \code{maxSize = 0}, any message that requires authorization will be immediately discarded. If message storage is full,
#' older messages will be removed, until either the allowed storage size is reached, or only one message remains. Default
#' value is \code{Inf}.
#' }
#' }
#' @section Methods:
#' \describe{
#' \item{\code{getMessageIds()}}{
#' Returns IDs of all currently stored messages. ID is combination of 6 random letters and numbers,
#' which is generated, when the message is stored. See also \code{\link{getMessageIds}}.
#' }
#' \item{\code{authorize(messageId = NULL, show = FALSE)}}{
#' Authorizes evaluation of the message. Check \code{\link{authorize}} for more information.
#' }
#' \item{\code{removeMessage(messageId = NULL)}}{
#' Removes a stored message. This can also be done with \code{\link{authorize}} function (set
#' \code{show = TRUE} and then select to ignore message). See also \code{\link{removeMessage}}.
#' }
#' \item{\code{sendCommand(command, wait = 0)}}{
#' Sends a JavaScript command to be evaluated on the web page. Check
#' \code{\link{sendCommand}} for more information.
#' }
#' \item{\code{callFunction(name, arguments = NULL, assignTo = NULL, wait = 0, thisArg = NULL, ...)}}{
#' Calls an existing JavaScript
#' function on the web page. Check \code{\link{callFunction}} for more information.
#' }
#' \item{\code{sendData(variableName, variable, wait = 0, keepAsVector = FALSE, rowwise = TRUE)}}{
#' Sends data and assigns it to
#' a variable on the web page. Check \code{\link{sendData}} for more information.
#' }
#' \item{\code{sendHTML(html, wait = 0)}}{
#' Sends HTML code that will be appended to the web page. Check \code{\link{sendHTML}} for
#' more information.
#' }
#' \item{\code{sessionVariables(vars = NULL, varName = NULL, remove = NULL)}}{
#' Sets or returns variables that are used (read or modified) only by this session. If both arguments are
#' \code{NULL}, returns environment for this session. If \code{vars} is a named list, adds this variables to the
#' session environment. If \code{varName} is a character, returns a variable with this name how it is seen from
#' the session. If the variable doesn't exist, throws an error. If \code{remove} is a vector of characters, removes
#' variables with these names from the session environment. One can add variables to the session environment,
#' get one back and remove variables with a single function call. Check \code{\link{setSessionVariables}},
#' \code{\link{getSessionVariable}}, \code{\link{removeSessionVariables}} for more information.
#' }
#' }
#' Note, that \code{Session} class has some other public methods that are not mentioned in this list. These methods are
#' intended to be used only by other functions of \code{jrc} package and therefore are not documented.
NULL
#' @importFrom stringi stri_rand_strings
#' @import stringr
#' @import R6
#' @importFrom jsonlite fromJSON
#' @importFrom utils object.size
#' @export
Session <- R6Class("Session", cloneable = FALSE, public = list(
id = "",
lastActive = NULL,
startTime = NULL,
maxN = Inf,
maxSize = Inf,
storeMessage = function(msg) {
if(self$maxN == 0 | self$maxSize == 0) {
message(str_c("Message can't be stored, sincse message storage is set to zero. ",
"Please, use 'limitStorage' function to change the limits."))
return()
}
if(!is.vector(msg))
stop("Unknown message format")
if(msg[1] == "COM") {
message(str_c("Command '", msg[2], "' is stored."))
} else if(msg[1] == "DATA") {
message(str_c("Assignment to the variable '", msg[2], "' is stored."))
} else if(msg[1] == "FUN") {
message(str_c("Call to the function '", msg[2], "' is stored."))
} else {
stop("Unknown message type. Must be one of 'COM', 'DATA' or 'FUN'")
}
messageId <- stri_rand_strings(1, 6)
private$storage[[messageId]] <- list(msg = msg, size = object.size(msg), id = messageId)
message(str_c("To authorize execution, please, type 'authorize(sessionId = \"", self$id,
"\", messageId = \"", messageId, "\")'"))
if(!private$waiting) {
self$callFunction("jrc.notifyStorage", list(messageId))
} else {
private$waiting <- FALSE
}
self$lastActive <- Sys.time()
private$cleanStorage()
},
execute = function(messageId = NULL, msg = NULL) {
if(is.null(msg))
if(is.null(messageId)) {
stop("Either message of message ID must be provided.")
} else {
msg <- self$getMessage(messageId)
}
private$waiting <- FALSE
if(is.null(msg))
stop(str_c("There is no message with ID ", messageId))
tryCatch({
if(msg[1] == "COM") {
eval(parse(text = msg[2]), envir = private$envir)
} else if(msg[1] == "DATA") {
# 1 = "DATA"
# 2 - variable name
# 3 - variable
# 4 - boolean (sessionwise or outer envir)
if(is.na(msg[[4]]))
msg[[4]] <- exists(msg[[2]], inherits = FALSE, envir = private$envir)
if(msg[[4]]) {
assign(msg[[2]], msg[[3]], envir = private$envir)
} else {
assign(msg[[2]], msg[[3]], envir = parent.env(private$envir))
}
} else if(msg[1] == "FUN") {
# 1 = "FUN"
# 2 - function name
# 3 - list of arguments
# 4 - assignTo
# 5 - package
# 6 - boolean (sessionwise or outer envir)
chain <- strsplit(msg[[2]], "[$]")[[1]]
if(!is.na(msg[[5]])){
env <- getNamespace(msg[[5]])
} else {
env <- private$envir
}
repeat {
f <- get(chain[1], envir = env)
chain <- chain[-1]
if(is.environment(f))
env <- f
if(length(chain) == 0)
break
}
if(identical(private$envir, env) && !isNamespace(environment(f))) {
environment(f) <- env
} else {
fors <- formals(f)
for(arg in setdiff(names(fors), names(msg[[3]])))
if(arg != "...")
tryCatch(msg[[3]][[arg]] <- eval(fors[[arg]], private$envir),
error = function(e) {})
}
tmp <- do.call(f, msg[[3]])
# parent.env(private$envir) <- outer
if(!is.na(msg[[4]])){
if(is.na(msg[[6]]))
msg[[6]] <- exists(msg[[4]], inherits = FALSE, envir = private$envir)
if(msg[[6]]) {
assign(msg[[4]], tmp, envir = private$envir)
} else {
assign(msg[[4]], tmp, envir = parent.env(private$envir))
}
}
}
}, finally = {
if(!is.null(messageId))
self$removeMessage(messageId)
self$lastActive <- Sys.time()
}
)
},
getMessage = function(messageId) {
if(!is.character(messageId))
stop("Message ID must be a string")
if(length(messageId) > 1) {
warning("An attepmt to supply several message IDs. Only the first one will be used")
messageId <- messageId[1]
}
msgObj <- private$storage[[messageId]]
if(!is.null(msgObj))
msgObj <- msgObj$msg
msgObj
},
removeMessage = function(messageId = NULL) {
if(length(private$storage) == 0)
stop("There are no stored messages")
if(is.null(messageId))
if(length(private$storage) > 1) {
stop("There is more than one stored message. Please, specify message ID")
} else {
messageId <- names(private$storage)
}
if(!is.character(messageId))
stop("Message ID must be a string")
if(length(messageId) > 1) {
warning("An attepmt to supply several message IDs. Only the first one will be used")
messageId <- messageId[1]
}
private$storage[[messageId]] <- NULL
invisible(self)
},
getMessageIds = function() {
sapply(private$storage, `[[`, "id")
},
sendCommand = function(command, wait = 0) {
if(is.null(private$ws))
stop("Websocket is already closed.")
stopifnot(is.character(command))
private$ws$send( toJSON(c("COM", command)) )
if(wait > 0)
private$wait(wait)
},
callFunction = function(name, arguments = NULL, assignTo = NULL, wait = 0, thisArg = NULL, ...) {
if(is.null(private$ws))
stop("Websocket is already closed.")
if(!is.character(name))
stop("Function name must be a character")
if(!is.null(assignTo) & !is.character(assignTo))
stop("Variable name in 'assignTo' must be a character")
if(!is.null(arguments)) {
if(!is.list(arguments))
stop("Arguments must be a list")
names(arguments) <- NULL
self$sendData("___args___", arguments, ...)
}
private$ws$send(toJSON(c("FUN", name, assignTo)))
if(wait > 0)
private$wait(wait)
},
sendData = function(variableName, variable, wait = 0, keepAsVector = FALSE, rowwise = TRUE) {
if(is.null(private$ws))
stop("Websocket is already closed.")
stopifnot(is.character(variableName))
if(length(variableName) > 1) {
warning("An attempt to supply multiple variable names. Only the first one will be used.")
variableName <- variableName[1]
}
if(rowwise) {
dataframe <- "rows"
matrix <- "rowmajor"
} else {
dataframe <- "columns"
matrix <- "columnmajor"
}
private$ws$send( toJSON(c("DATA", variableName,
toJSON(variable, digits = NA, dataframe = dataframe, matrix = matrix),
keepAsVector)))
if(wait > 0)
private$wait(wait)
},
sendHTML = function(html, wait = 0) {
if(is.null(private$ws))
stop("Websocket is already closed.")
stopifnot(is.character(html))
private$ws$send( toJSON(c("HTML", html)) )
if(wait > 0)
private$wait(wait)
},
authorize = function(messageId = NULL, show = FALSE) {
if(is.null(messageId)) {
if(length(private$storage) > 1)
stop("More than one message is stored for this session. Please, specify message ID.")
if(length(private$storage) == 0)
stop("There are no stored messages for this session")
messageId <- names(private$storage)
}
if(!is.logical(show))
stop("show must be a logical variable")
if(!show) {
self$execute(messageId)
} else {
msg <- self$getMessage(messageId)
if(is.null(msg))
stop(str_c("There is no message with ID ", messageId))
if(msg[1] == "COM") {
text <- str_c("Command '", msg[2], "'.")
} else if(msg[[1]] == "DATA") {
text <- str_c("Assignment of varible '", msg[[2]],
"'. New type is '", msg[[3]], "'. ",
"New size is ", msg[[3]], " bytes.")
} else if(msg[[1]] == "FUN") {
text <- str_c("Call of function '", msg[[2]], "'.")
if(!is.na(msg[[4]]))
text <- str_c(text, " Results will be assigned to variable '", msg[[4]], "'.")
}
text <- str_c(text, " To cancel enter '0'.")
choice <- menu(c("Execute", "Ignore"),
title = text)
if(choice == 0) return()
if(choice == 1) self$execute(messageId)
}
invisible(self)
},
sessionVariables = function(vars = NULL, varName = NULL, remove = NULL) {
if(is.null(vars) && is.null(varName) && is.null(remove))
return(private$envir)
if(!is.null(vars) & length(vars) > 0){
if(!is.list(vars) || is.null(names(vars)))
stop("Session variables must be a named list")
list2env(vars, private$envir)
}
if(!is.null(varName)){
if(!is.character(varName))
stop("Variable name must be a character")
if(length(varName) > 1) {
warning("Can't get several variables at once. Only the first variable name will be used")
varName <- varName[1]
}
return(get(varName, envir = private$envir))
}
if(!is.null(remove)){
if(!is.character(remove))
stop("Variable names must be characters")
stopifnot(is.vector(remove))
rm(list = remove, envir = private$envir)
}
invisible(self)
},
setEnvironment = function(envir) {
stopifnot(is.environment(envir))
parent.env(private$envir) <- envir
invisible(self)
},
close = function(message = NULL) {
if(!is.null(message)) {
if(!is.character(message))
stop("Closing message must be a string.")
self$sendCommand(str_c("alert('", mesage, "');"))
}
if(!is.null(private$ws))
private$ws$close()
},
initialize = function(ws, envir = NULL) {
self$id <- stri_rand_strings(1, 6)
if(is.null(envir))
envir <- new.env()
stopifnot(is.environment(envir))
private$envir <- envir
self$lastActive <- Sys.time()
self$startTime <- Sys.time()
private$ws <- ws
self$sessionVariables(list(.id = self$id))
}
), private = list(
ws = NULL,
envir = NULL,
storage = list(),
waiting = FALSE,
cleanStorage = function() {
if(length(private$storage) > self$maxN){
message(str_c("Too many messages! Message with id '", private$storage[[1]]$id, "' removed"))
private$storage[1] <- NULL
}
while(sum(sapply(private$storage, `[[`, "size")) > self$maxSize &
length(private$storage) > 1){
message(str_c("Messages size is too big! Message with id '", private$storage[[1]]$id, "' removed"))
private$storage[1] <- NULL
}
},
wait = function(time) {
private$waiting <- TRUE
for( i in 1:(time/0.05) ) {
service(100)
if( !private$waiting ){
break
}
Sys.sleep( .05 )
}
if(private$waiting)
warning(str_c("Failed to receive response from the websocket. Session ID: ", self$id))
private$waiting <- FALSE
}
))
#' @name App
#' @title App class
#'
#' @description Object of this class represents the entire jrc-based app. It stores all the active connections,
#' client-specific variables and all the global app settings.
#'
#' You can create interactive apps by initializing
#' new instances of this class and manage the apps with the methods that are described below. There are no limitations
#' on the number of apps that can run simultaneously in one R session.
#'
#' A wrapper function is also exported for almost each method (see links in the Methods section). This functions allow
#' you to gain full control over the app without ever dealing with this class. However, in this case only a single app
#' can run per R session. Attempt to create a new app (with \code{\link{openPage}} function) will force the existing one (if any)
#' to stop. You can always get the \code{App} object for the currently running app with \code{\link{getPage}} function.
#'
#' @section Methods:
#' \describe{
#' \item{\code{new(rootDirectory = NULL, startPage = NULL, onStart = NULL,
#' connectionNumber = Inf, allowedFunctions = c(), allowedVariables = c(), sessionVars = NULL)}}{
#' Creates a new instance of class \code{App}. Check \code{\link{openPage}} man page for information about
#' arguments.
#' }
#' \item{\code{startServer(port = NULL)}}{
#' Starts a local server that listens to a given port. If \code{port = NULL}, picks a random available port.
#' See also \code{\link{openPage}}.
#' }
#' \item{\code{stopServer()}}{
#' Closes all active sessions and stops a running server. See also \code{\link{closePage}}.
#' }
#' \item{\code{openPage(useViewer = TRUE, browser = NULL)}}{
#' Opens a new web page either in a browser, or in the R Studio viewer. If \code{useViewer = FALSE} and browser is not selected,
#' a default installed browser is used. If browser is specified, \code{useViewer} is ignored. This method returns
#' a new \code{\link{Session}} object, which should correspond to the page that has been just opened. However, if someone would start
#' a new connection at the moment when \code{openPage} method is called, it may return a wrong session. See also \code{\link{openPage}}.
#' }
#' \item{\code{getSession(sessionId = NULL)}}{
#' Returns a session with the given ID or \code{NULL} if session with this ID doesn't exist. If \code{sessionId = NULL}
#' and there is only one active session, returns it. See also \code{\link{getSession}}.
#' }
#' \item{\code{closeSession(sessionId = NULL, inactive = NULL, old = NULL)}}{
#' Closes websocket connection of one or multiple sessions and removes all the related data from the app. For more information on
#' the arguments, please, check \code{\link{closeSession}} man page.
#' }
#' \item{\code{getSessionIds()}}{
#' Returns IDs of all currently active sessions. See also \code{\link{getSessionIds}}.
#' }
#' \item{\code{setEnvironment(envir)}}{
#' Specifies the outer environment of the app, in which all the messages from the web pages will be evaluated. For more information,
#' please, check \code{\link{setEnvironment}}.
#' }
#' \item{\code{allowFunctions(funs = NULL)}}{
#' Adds function names to a list of allowed R functions. These functions can be called from a web page without authorization
#' on the R side. If \code{funs = NULL}, returns a list of all currently allowed functions. For more information,
#' please, check \code{\link{allowFunctions}}.
#' }
#' \item{\code{allowVariables(vars)}}{
#' Adds variable names to the list of allowed variables. These variables can be changed from a web page without
#' authorization on the R side. If \code{vars = NULL}, then returns a vector of names of all currently allowed variables.
#' For more information, please, check \code{\link{allowVariables}}.
#' }
#' \item{\code{startPage(path = NULL)}}{
#' Sets path to a starting web page of the app. Path can be full, relative to the app's root directory or relative
#' to the current R working directory. If
#' \code{path = NULL}, returns current path to the starting page.
#' }
#' \item{\code{rootDirectory(path = NULL)}}{
#' Sets path to the root directory for the server. Any file, requested by the server, will be looked for in this directory.
#' Can be a full path or a path relative to the current R working directory. If \code{path = NULL}, returns path to the
#' current root directory.
#' }
#' \item{\code{numberOfConnections(maxCon = NULL)}}{
#' Sets maximum number of connections that can be active simultaneously. If \code{maxCon = NULL}, returns current value of
#' maximum allowed number of connections. Default value is \code{Inf}.
#' }
#' }
#'
NULL
#' @import mime
#' @export
App <- R6Class("App", cloneable = FALSE, public = list(
addSession = function(session) {
stopifnot("Session" %in% class(session))
if(length(private$sessions) >= private$maxCon) {
session$close("Maximum number of active connections has been reached.")
stop("Maximum number of connections has been reached. Please, close some of
the existing sessions, before adding a new one.")
}
oldSession <- self$getSession(session$id)
if(!is.null(oldSession)) {
warning(str_c("Session with id ", session$id, " already exists. Existing session will be closed."))
self$closeSession(oldSession)
}
private$sessions[[session$id]] <- session
},
getSession = function(sessionId = NULL) {
if(is.null(sessionId))
if(length(private$sessions) <= 1) {
return(private$sessions[[1]])
} else {
stop("There are more than one active session. Please, specify session ID.")
}
if(!is.character(sessionId))
stop("Session ID must be a string")
private$sessions[[sessionId]]
},
closeSession = function(sessionId = NULL, inactive = NULL, old = NULL) {
if(length(private$sessions) == 0)
stop("There are no active sessions.")
if(is.null(sessionId))
if(is.null(inactive) & is.null(old)){
if(length(private$sessions) > 1)
stop("There is more than one active session. Please, specify ID of the session you want to close")
sessionId <- names(private$sessions)
}
stopifnot(is.vector(sessionId) | is.null(sessionId))
if(!is.null(inactive)) {
lastActive <- sapply(private$sessions, `[[`, "lastActive")
rem <- (lastActive < Sys.time() - inactive)
sessionId <- unique(c(sessionId, names(lastActive)[rem]))
}
if(!is.null(old)) {
startTime <- sapply(private$sessions, `[[`, "startTime")
rem <- (startTime < Sys.time() - old)
sessionId <- unique(c(sessionId, names(startTime)[rem]))
}
for(id in sessionId) {
session <- self$getSession(id)
if(!is.null(session)){
session$close()
private$sessions[[id]] <- NULL
} else {
warning(str_c("There is no session with ID ", id))
}
}
invisible(self)
},
getSessionIds = function() {
names(private$sessions)
},
stopServer = function() {
lapply(names(private$sessions), self$closeSession)
if(!is.null(private$serverHandle)) {
if(compareVersion(as.character(packageVersion("httpuv")), "1.3.5") > 0) {
stopServer(private$serverHandle)
} else {
stopDaemonizedServer(private$serverHandle)
}
message("Server has been stopped.")
}
invisible(self)
},
startServer = function(port = NULL) {
if(is.null(port)) {
if(compareVersion(as.character(packageVersion("httpuv")), "1.5.4") >= 0){
port <- randomPort(n = 50)
} else {
#if there is no randomPort function in the httpuv package
#in later versions of jrc this will be removed and httpuv >= 1.5.2 will be required
#code adopted from httpuv::randomPort
for (port in sample(seq(1024L, 49151L), 50)) {
s <- NULL
# Check if port is open
tryCatch(
s <- startServer("0.0.0.0", port, list(), quiet = TRUE),
error = function(e) { }
)
if (!is.null(s)) {
s$stop()
break
}
}
}
}
port <- as.integer(port)
if(is.na(port))
stop("Port number must be an integer number.")
private$port <- port
if(!(compareVersion(as.character(packageVersion("httpuv")), "1.3.5") > 0)) {
private$serverHandle <- startDaemonizedServer( "0.0.0.0", private$port, private$getApp() )
} else {
private$serverHandle <- startServer( "0.0.0.0", private$port, private$getApp() )
}
invisible(self)
},
openPage = function(useViewer = TRUE, browser = NULL) {
if(!is.null(browser))
useViewer <- FALSE
if(is.null(private$serverHandle))
stop("No server is running. Please, start a server before opening a page.")
if( useViewer & !is.null( getOption("viewer") ) )
getOption("viewer")( str_c("http://localhost:", private$port, private$startP) )
else{
if(is.null(browser))
browser = getOption("browser")
browseURL( str_c("http://localhost:", private$port, private$startP), browser = browser )
}
# Wait up to 5 seconds for the a websocket connection
# incoming from the client
private$waiting <- TRUE
for( i in 1:(5/0.05) ) {
service(100)
if( !private$waiting ){
break
}
Sys.sleep( .05 )
}
if( private$waiting ) {
self$stopServer()
stop( "Timeout waiting for websocket." )
}
invisible(private$sessions[[length(private$sessions)]])
},
setEnvironment = function(envir, sessionId = NULL) {
stopifnot(is.environment(envir))
private$envir <- envir
if(is.null(sessionId))
sessionId <- names(private$sessions)
if(!is.null(sessionId)) {
stopifnot(is.vector(sessionId))
for(id in sessionId)
self$getSession(id)$setEnvironment(envir)
}
invisible(self)
},
allowFunctions = function(funs = NULL) {
if(is.null(funs)) return(private$allowedFuns)
if(!is.vector(funs) | !is.character(funs))
stop("'funs' must be a vector of function names")
private$allowedFuns <- unique(c(private$allowedFuns, funs))
invisible(self)
},
allowVariables = function(vars = NULL) {
if(is.null(vars)) return(private$allowedVars)
if(!is.vector(vars) | !is.character(vars))
stop("'funs' must be a vector of function names")
private$allowedVars <- unique(c(private$allowedVars, vars))
invisible(self)
},
rootDirectory = function(path = NULL) {
if(is.null(path)) return(private$rootDir)
stopifnot(is.character(path))
if(!dir.exists(path))
stop(str_c("There is no such directory: '", path, "'"))
private$rootDir <- normalizePath(path, winslash = .Platform$file.sep)
invisible(self)
},
startPage = function(path = NULL) {
if(is.null(path)) {
if(is.null(private$startPagePath)){
return(path)
} else {
return(str_c(private$startPagePath, private$startP))
}
}
if(file.exists(file.path(private$rootDir, path))){
if(substring(path, 1, 1) != .Platform$file.sep)
path <- str_c(.Platform$file.sep, path)
private$startP <- path
} else {
if(!file.exists(path))
stop(str_c("There is no such file: '", path, "'"))
path <- normalizePath(path, winslash = .Platform$file.sep)
if(grepl(path, private$rootDir, fixed = T)) {
private$startP <- str_remove(path, private$rootDir)
} else {
spl <- strsplit(path, .Platform$file.sep)
private$startP <- str_c(.Platform$file.sep, spl[[1]][length(spl[[1]])])
private$startPagePath <- str_remove(path, private$startP)
}
}
},
numberOfConnections = function(maxCon = NULL) {
if(is.null(maxCon))
return(private$maxCon)
stopifnot(is.numeric(maxCon))
private$maxCon <- maxCon
invisible(self)
},
sessionVariables = function(vars = NULL) {
if(is.null(vars))
return(private$sessionVars)
vars <- as.list(vars)
if(!is.list(vars))
stop("Variables must be a list")
if(is.null(names(vars)))
stop("List of variables must be named")
for(n in names(vars))
private$sessionVars[[n]] <- vars[[n]]
invisible(self)
},
initialize = function(rootDirectory = NULL, startPage = NULL, onStart = NULL,
connectionNumber = Inf, allowedFunctions = c(),
allowedVariables = c(), sessionVars = NULL) {
if(is.null(rootDirectory))
rootDirectory <- system.file("http_root", package = "jrc")
self$rootDirectory(rootDirectory)
if(is.null(startPage))
startPage <- system.file("http_root/index.html", package = "jrc")
self$startPage(startPage)
private$envir <- parent.frame(n = 2)
if(is.null(onStart)) {
onStart <- function(session) {}
}
stopifnot(is.function(onStart))
private$onStart <- onStart
self$allowFunctions(allowedFunctions)
self$allowVariables(allowedVariables)
self$sessionVariables(sessionVars)
self$numberOfConnections(connectionNumber)
invisible(self)
}
), private = list(
sessions = list(),
serverHandle = NULL,
envir = NULL,
allowedFuns = c(),
allowedVars = c(),
maxCon = Inf,
port = NULL,
waiting = FALSE,
onStart = NULL,
rootDir = "",
startP = "",
startPagePath = NULL,
sessionVars = list(),
getApp = function() {
handle_http_request <- function( req ) {
reqPage <- req$PATH_INFO
if(grepl("^/http_root", reqPage)) {
pack <- substring(strsplit(reqPage, "/")[[1]][2], 11)
reqPage <- sub(str_c("_", pack), "", reqPage)
reqPage <- tryCatch(system.file( reqPage, package = pack, mustWork = TRUE ),
error = function(e) system.file( paste0("inst/", reqPage), package = pack))
} else {
if(reqPage == "/index.html" || reqPage == "/")
reqPage <- private$startP
if(reqPage == private$startP && !is.null(private$startPagePath)) {
reqPage <- str_c(private$startPagePath, private$startP)
} else {
reqPage <- str_c(private$rootDir, reqPage)
}
}
if( !file.exists(reqPage) ) {
reqPage <- str_remove(reqPage, private$rootDir)
if(!file.exists(reqPage)) {
warning(str_interp("File '${reqPage}' is not found"))
return( list(
status = 404L,
headers = list( "Content-Type" = "text/html" ),
body = "404: Resource not found" ) )
}
}
content_type <- mime::guess_type(reqPage)
content <- readLines(reqPage, warn = F)
if(content_type == "text/html") {
jsfile <- str_c("")
stop <- F
for(i in 1:length(content))
if(str_detect(content[i], regex("]*>)", ignore_case = T), str_c("\\1", jsfile))
}
#the document has no tag
if(!stop) {
jsfile <- str_c("", jsfile, "")
for(i in 1:length(content))
if(str_detect(content[i], regex("]*>)", ignore_case = T), str_c("\\1", jsfile))
}
}
if(!stop)
content <- c(jsfile, content)
}
list(
status = 200L,
headers = list( 'Content-Type' = content_type ),
body = str_c( content, collapse="\n" )
)
}
handle_websocket_open <- function( ws ) {
session <- Session$new(ws, envir = new.env(parent = private$envir))
session$sessionVariables(private$sessionVars)
ws$onMessage( function( isBinary, msg ) {
if( isBinary )
stop( "Unexpected binary message received via WebSocket" )
msg <- fromJSON(msg)
if(!(msg[1] %in% c("COM", "FUN", "DATA")))
stop(str_interp("Unknown message type: ${msg[1]}"))
if(msg[1] == "COM") {
session$storeMessage(msg) #vector of characters
}
if(msg[1] == "DATA") {
if(!is.character(msg[2]))
stop("Invalid message structure. Variable name is not character.")
msg <- as.list(msg)
msg[[3]] <- fromJSON(msg[[3]])
if(msg[[2]] %in% private$allowedVars) {
session$execute(msg = msg)
} else {
session$storeMessage(msg)
}
}
if(msg[1] == "FUN") {
if(!is.character(msg[2]))
stop("Invalid message structure. Function name is not character.")
#make sure that function arguments is a list
msg <- as.list(msg)
if(!is.na(msg[[3]]))
msg[[3]] <- fromJSON(msg[[3]])
if(is.vector(msg[[3]]))
msg[[3]] <- as.list(msg[[3]])
if(length(msg[[3]]) == 1 && is.na(msg[[3]]))
msg[[3]] <- list()
msg[[3]] <- as.list(msg[[3]])
if(!is.list(msg[[3]]))
stop("Invalid message structure. List of arguments is not a list.")
#go through all arguments and turn to numeric
if(msg[[2]] %in% private$allowedFuns & (is.na(msg[[4]]) | msg[[4]] %in% private$allowedVars)) {
session$execute(msg = msg)
} else {
session$storeMessage(msg)
}
}
} );
ws$onClose(function() {
if(!is.null(self$getSession(session$id)))
self$closeSession(session$id)
})
ws$send(toJSON(c("ID", session$id)))
session$sessionVariables(private$sessionVars)
self$addSession(session)
private$onStart(session)
private$waiting <- FALSE
}
list(call = handle_http_request,
onWSOpen = handle_websocket_open)
}
))
pkg.env <- new.env()
#' Create a server
#'
#' \code{openPage} starts a server and opens a new page with a websocket connection between it and the current
#' R session. After that, messages can be exchanged between R session and the web page to generate content on the
#' web page and to trigger calculations in R as a response to user activity on the page.
#'
#' \code{jrc} supports four types of messages:
#' \itemize{
#' \item{Commands are pieces of R or JavaScript code that will be evaluated on the receiving side. Note,
#' that any command from a web page must be authorized in the R session for security reasons. A message
#' with information about how to do that is printed in the console each time a command is received. For more
#' information, please, check \code{\link{sendCommand}}.}
#' \item{Data is any variable that is sent to or from the R session. It must always come with a
#' name of the variable to which it should be assigned on the receiving side. For more information, please,
#' check \code{\link{sendData}}.}
#' \item{Function calls can be triggered on each side of the websocket connection. Alongside the function name,
#' one can also send a list of arguments and name of a variable to which the returned value of the function will
#' be assigned. For more information, please, check \code{\link{callFunction}}.}
#' \item{Unlike other types of messages, HTML code can be sent only from the R session to a web page. This code will
#' be added to the body of the page.}
#' }
#'
#' \code{openPage} function is a wrapper around several methods of class \code{\link{App}}. First, it creates an
#' instance of this class. Then it starts a server that listens to the given port. And finally, it attempts
#' to open a new web page. It also stores a new app object in the package namespace, which allows other
#' wrapper functions to access it.
#'
#' @param useViewer If \code{TRUE}, the new web page will be opened in the RStudio Viewer. If \code{FALSE},
#' a default web browser will be used (if other is not specified with the \code{browser} argument).
#' @param rootDirectory A path to the root directory fpr the server. Any file, requested by the server
#' will be searched for in this directory. If \code{rootDirectory} is not
#' defined, the \code{http_root} in the package directory will be used as a root directory.
#' @param startPage A path to an HTML file that should be used as a starting page of the app.
#' It can be an absolute path to a local file, or it can be relative to the \code{rootDirectory}
#' or to the current R working directory. If \code{startPage} is not defined, an empty page will be used.
#' The file must have \emph{.html} extension.
#' @param port Defines which TCP port the server will listen to. If not defined, random available port
#' will be used (see \code{\link[httpuv]{randomPort}}).
#' @param browser A browser in which to open a new web page.
#' If not defined, default browser will be used. For more information check \code{\link[utils]{browseURL}}.
#' If this argument is specified, \code{useViewer} will be ignored.
#' @param allowedFunctions List of functions that can be called from a web page without any additional actions
#' on the R side. All other functions will require authorization in the current R session before they are called.
#' This argument should be a vector of R function names. Check \code{\link{authorize}} and \code{\link{allowFunctions}}
#' for more information.
#' @param allowedVariables List of variables that can be modified from a web page without any additional actions
#' on the R side. All other variable reassignments must be confirmed in the current R session.
#' This argument should be a vector of variable names. Check \code{\link{authorize}} and \code{\link{allowVariables}}
#' for more information.
#' @param connectionNumber Maximum number of connections that is allowed to be active simultaneously.
#' @param sessionVars Named list of variables, that will be declared for each session, when a new connection is opened.
#' Any changes to these variables will affect only a certain session. Thus they can be used, for instance, to
#' store a state of each session. For more information, please, check \code{\link{setSessionVariables}}.
#' @param onStart A callback function that will be executed when a new connection is opened. This function gets a single
#' argument, which is an object of class \code{\link{Session}}. General purpose of the function is to populate each
#' new web page with some default content.
#'
#' @seealso \code{\link{closePage}}, \code{\link{setEnvironment}}, \code{\link{limitStorage}}, \code{\link{allowVariables}},
#' \code{\link{allowFunctions}}, \code{\link{setSessionVariables}}.
#'
#' @return Object of class \code{\link{App}}.
#'
#' @export
#' @import httpuv
#' @importFrom utils browseURL
#' @importFrom utils compareVersion
#' @importFrom utils packageVersion
openPage <- function(useViewer = TRUE, rootDirectory = NULL, startPage = NULL, port = NULL, browser = NULL,
allowedFunctions = NULL, allowedVariables = NULL, connectionNumber = Inf, sessionVars = NULL,
onStart = NULL) {
if(!is.null(pkg.env$app))
closePage()
app <- App$new(rootDirectory, startPage, onStart, connectionNumber, allowedFunctions, allowedVariables, sessionVars)
pkg.env$app <- app
app$setEnvironment(parent.frame())
app$startServer(port)
app$openPage(useViewer, browser)
invisible(app)
}
sendMessage <- function(type, id, ...) {
if(is.null(pkg.env$app))
stop("There is no opened page. Please, use 'openPage()' function to create one.")
if(is.null(id))
id <- pkg.env$app$getSessionIds()
for(i in id){
session <- pkg.env$app$getSession(i)
if(is.null(session)) {
warning(str_c("There is no session with ID ", i))
} else {
tryCatch(session[[type]](...),
error = function(e) {
if(e$message == "Websocket is already closed.") {
pkg.env$app$closeSession(session)
stop(str_c("Websocket is already closed.",
"Session ", session$id, " has been terminated."))
} else {
stop(e)
}
})
}
}
}
#' Send a command to a web page
#'
#' \code{sendCommand} sends JavaScript code through the selected websocket connection and evaluates it on the specified
#' web page. Use JavaScript function \code{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 \code{\link{authorize}}).
#' @details Each opened page gets its own environment, where all the commands are evaluated. Any changes
#' made with the usual assignment operator \code{<-} will be limited to this page-specific environment. The changes
#' are still saved, but can be accessed only with \code{\link{getSessionVariable}} function. To make changes outside
#' of the page-specific environment use \code{<<-} instead.
#'
#' In JavaScript one should use \code{windows.varibleName = "SomeValue"}
#' instead of \code{varibleName = "SomeValue"}, in order to make the variable accessible outside of the
#' current \code{sendCommand} call.
#'
#' This function is a wrapper around \code{sendCommand} method of class \code{\link{Session}}.
#'
#' @param command A line (or several lines separated by \code{\\n}) of JavaScript code. This code
#' will be directly evaluated on the web page. No R-side syntax check is performed.
#' @param sessionId An ID of the session to which the command should be sent. Can also be a vector of multiple session IDs.
#' If \code{NULL}, the command will be sent to all currently active sessions.
#' @param wait If \code{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.
#'
#' @examples
#' \donttest{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()}
#'
#' @seealso \code{\link{authorize}}, \code{\link{sendData}}, \code{\link{sendHTML}}, \code{\link{callFunction}},
#' \code{\link{openPage}}, \code{\link{getSessionIds}}.
#'
#' @export
#' @importFrom jsonlite toJSON
sendCommand <- function(command, sessionId = NULL, wait = 0) {
sendMessage("sendCommand", sessionId, wait = wait, command = command)
}
#' Stop server
#'
#' Stops the server and closes all currently opened pages (if any). This function is a
#' wrapper of \code{stopServer} method of class \code{\link{App}}.
#'
#' @seealso \code{\link{openPage}}
#'
#' @export
closePage <- function() {
if(!is.null(pkg.env$app)) {
pkg.env$app$stopServer()
pkg.env$app <- NULL
} else {
message("There is no opened page.")
}
}
#' Send data to a web page
#'
#' Sends a variable to a web page, where it is saved under a specified name. This function
#' is a wrapper around \code{sendData} method of class \code{\link{Session}}.
#'
#' To send data back from the web page to the current R session one should use\code{jrc.sendData(variableName, variable, internal)}.
#' Its arguments are:
#'
#' \describe{
#' \item{\code{variableName}}{
#' Name that the variable will have in the R session. If variable name hasn't been previously added to the list
#' of allowed variables (see \code{\link{allowVariables}} or \code{allowedVariables} argument of the \code{\link{openPage}}
#' function), attempt to assign it from a web page will require manual authorization on the R side.
#' }
#' \item{\code{variable}}{
#' Variable to send.
#' }
#' \item{\code{internal} (optional)}{
#' Whether this variable should be used only by the session that sent it. If \code{true}, variable will be stored
#' in the session-specific environment and can be accessed from the outside with \code{\link{getSessionVariable}}
#' function. If \code{false}, variable will be saved to the outer environment of the app (see \code{\link{setEnvironment}}).
#' By default, uses \code{true} for variables that already exist in the session specific environment
#' (see \code{\link{setSessionVariables}} or \code{sessionVariables} argument of the \code{\link{openPage}} function.)
#' and \code{false} otherwise.
#' }
#' }
#'
#' @param variableName Name that the variable will have on the web page.
#' @param variable Variable to send.
#' @param keepAsVector If \code{TRUE}, variables with length 1 will be saved as arrays on the web page, otherwise they
#' will be converted to atomic types.
#' @param rowwise If \code{TRUE}, matrices and data.frames will be transformed into JavaScript objects or arrays
#' row wise (e.g. a matrix will become an Array of its rows).
#' @param sessionId An ID of the session to which the data should be sent. Can also be a vector of multiple session IDs.
#' If \code{NULL}, the data will be sent to all currently active sessions.
#' @param wait If \code{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.
#'
#' @examples
#' \donttest{openPage()
#' x <- 1:100
#' sendData("x", x)
#' sendCommand("console.log(x);")
#' sendCommand("jrc.sendData('x', x.filter(function(e) {return e % 2 == 0}))")
#' closePage()}
#'
#' @seealso \code{\link{authorize}}, \code{\link{allowVariables}}, \code{\link{sendCommand}},
#' \code{\link{callFunction}}, \code{\link{sendHTML}}, \code{\link{openPage}}, \code{\link{getSessionIds}}.
#'
#' @export
#' @importFrom jsonlite toJSON
sendData <- function(variableName, variable, keepAsVector = FALSE, rowwise = TRUE, sessionId = NULL, wait = 0) {
sendMessage("sendData", sessionId, wait = wait, variableName = variableName, variable = variable, keepAsVector = keepAsVector,
rowwise = rowwise)
}
#' Set Environment
#'
#' Defines the outer environment of the app. Outer environment is a parent for all session environments.
#' It is used to store variables that are common for all the client sessions. The only way to make changes outside of
#' the outer environment is to use the global assignment operator \code{<<-} if and only if changes are
#' made to the variable that does not exist in the outer environment.
#'
#' By default, an environment where app was initialized (via \code{\link{openPage}} function or with \code{App$new()} call)
#' is used.
#'
#' This function is a wrapper around \code{setEnvironment} method of class \code{\link{App}}.
#'
#' @param envir Environment to be used as outer environment.
#'
#' @examples
#' \donttest{
#' openPage()
#' e <- new.env()
#' setEnvironment(e)
#'
#' sendCommand("jrc.sendData('x', 10)", wait = 3)
#' print(e$x)
#' closePage()
#' }
#'
#' @export
setEnvironment <- function(envir) {
if(is.null(pkg.env$app))
stop("There is no opened page. Please, use 'openPage()' function to create one.")
pkg.env$app$setEnvironment(envir)
}
#' Send HTML to a web page
#'
#' Sends a piece of HTML code to a web page and adds it at the end
#' or the \code{body} element. This function is a wrapper around \code{sendHTML} method of
#' class \code{\link{Session}}.
#'
#' @param html HTML code that will be added to the web page.
#' @param sessionId An ID of the session to which the HTML should be sent. Can also be a vector of multiple session IDs.
#' If \code{NULL}, the HTML will be sent to all currently active sessions.
#' @param wait If \code{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.
#'
#' @examples
#' \donttest{
#' openPage(FALSE)
#'
#' sendHTML("Test...")
#' sendHTML("This is bold")
#' sendHTML("")}
#'
#' @seealso \code{\link{sendData}}, \code{\link{sendCommand}}, \code{\link{callFunction}},
#' \code{\link{openPage}}.
#'
#' @export
sendHTML <- function(html = "", sessionId = NULL, wait = 0) {
sendMessage("sendHTML", sessionId, wait = wait, html = html)
}
#' Trigger a function call
#'
#' Calls a function in a web page by its name. It can also pass a list of arguments for the function and
#' save the returned result to a variable.
#'
#' JavaScript counterpart is \code{jrc.callFunction(name, arguments, assignTo, package, internal)}.
#' Its arguments are:
#' \describe{
#' \item{\code{name}}{
#' Name of an R function. If function name hasn't been previously added to the list
#' of allowed functions (see \code{\link{allowFunctions}} or \code{allowedFunctions} argument of \code{\link{openPage}}),
#' attempt to call it from a web page will require manual authorization on the R side.
#' }
#' \item{\code{arguments} (optional)}{
#' arguments for the function. This should be an Array (for unnamed arguments) or an Object with argument names as keys
#' (for named arguments).
#' }
#' \item{\code{assignTo} (optional)}{
#' Name of the variable to which the returned value of the function will be assigned in the R session.
#' If the variable name hasn't been previously added to the list
#' of allowed variables (see \code{\link{allowVariables}} or \code{allowedVariables} argument of \code{\link{openPage}}),
#' attempt to assign it from a web page will require manual authorization on the R side.
#' }
#' \item{\code{package} (optional)}{
#' If the function needs to be imported from an installed package, name of this package.
#' }
#' \item{\code{internal} (optional)}{
#' Whether assignment of the function returned value should happen internally or not. If \code{true}, result will be stored
#' in the session environment and can be accessed from the outside with \code{\link{getSessionVariable}}
#' function. If \code{false}, result will be saved to the outer environment of the app (see \code{\link{setEnvironment}}).
#' By default, uses \code{true} for variables that already exist in the session environment
#' (see \code{\link{setSessionVariables}} or \code{sessionVariables} argument of the \code{\link{openPage}} function)
#' and \code{false} otherwise.
#' }
#' }
#'
#' This function is a wrapper
#' around \code{callFunction} method of class \code{\link{Session}}.
#'
#' @param name Name of the function. If the function is a method of some object
#' its name must contain the full chain of calls (e.g. \code{myArray.sort} or
#' \code{Math.rand}).
#' @param arguments List of arguments for the function. Note that in JavaScript
#' arguments must be given in a fixed order, naming is not necessary and will
#' be ignored.
#' @param assignTo Name of a variable to which will be assigned the returned value
#' of the called function.
#' @param sessionId An ID of the session to which the function call should be sent. Can also be a vector of multiple
#' session IDs. If \code{NULL}, the function call will be sent to all currently active sessions.
#' @param wait If \code{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.
#' @param thisArg JavaScript functions (methods) can belong to some object, which
#' is referred to as \code{this} inside the function (e.g. in
#' \code{someObject.myFunction()} function \code{myFunction} is a method of \code{someObject}).
#' \code{thisArg} specifies object that will be known as \code{this} inside the function. If \code{NULL},
#' the function will be applied to the global object (\code{window}).
#' @param ... further arguments passed to \code{\link{sendData}}. It is used to send
#' \code{arguments} to the web page.
#'
#' @examples
#' \donttest{
#' openPage()
#' callFunction("alert", list("Some alertText"))
#' callFunction("Math.random", assignTo = "randomNumber")
#' sendCommand("alert(randomNumber)")
#' }
#'
#' @seealso \code{\link{authorize}}, \code{\link{allowFunctions}}, \code{\link{allowVariables}},
#' \code{\link{setEnvironment}}, \code{\link{getSessionIds}}.
#'
#' @export
callFunction <- function(name, arguments = NULL, assignTo = NULL, wait = 0, sessionId = NULL, thisArg = NULL, ...) {
sendMessage("callFunction", sessionId, wait = wait, name = name, arguments = arguments, assignTo = assignTo, thisArg = thisArg,
...)
}
#' Authorize further message processing
#'
#' \code{jrc} library allows one to get full control over the currently running R session from
#' a web page. Therefore for security reasons one should manually authorize function calls,
#' variable assignments or expression evaluations. All the received messages that are not
#' processed automatically are given an ID and stored. This function allows a message with the
#' given ID to be evaluated. It can also show a short description of the message and give user
#' a choice between running it or discarding.
#'
#' Expressions has to be always authorized before evaluation. One can specify a list of
#' variables that can be changed automatically and functions that can be called without
#' authorization.
#'
#' This function is a wrapper around \code{authorize} method of class \code{\link{Session}}.
#'
#' @param sessionId ID of the session that received the message. If there is only one active session, this
#' argument becomes optional.
#' @param messageId ID of the message to be processed. If the session has only one stored message, this argument becomes
#' optional.
#' @param show If \code{TRUE} information about the message will be shown first. After that user gets
#' a choice to go on with evaluation, to ignore the message (meaning it will be removed from memory) or to
#' do nothing.
#'
#' @examples
#' \donttest{openPage()
#'
#' callFunction("jrc.sendCommand", list("k <<- 10"), wait = 1)
#' allowVariables("x")
#' callFunction("jrc.sendData", list("x", 15), wait = 1)
#' callFunction("jrc.sendData", list("y", 20), wait = 1)
#' msgId <- getMessageIds()
#' authorize(messageId = msgId[1])
#' #run that to first see some information about the message
#' #authorize(messageId = msgId[2], show = TRUE)
#'
#' closePage()}
#' @seealso \code{\link{allowFunctions}}, \code{\link{allowVariables}}, \code{\link{limitStorage}}, \code{\link{getSessionIds}},
#' \code{\link{getMessageIds}}.
#'
#' @export
#' @importFrom utils menu
authorize <- function(sessionId = NULL, messageId = NULL, show = FALSE) {
if(is.null(pkg.env$app))
stop("There is no opened page. Please, use 'openPage()' function to create one.")
session <- pkg.env$app$getSession(sessionId)
if(is.null(session))
stop(str_c("There is no session with ID ", sessionId))
session$authorize(messageId, show)
}
#' Allow function calls without authorization
#'
#' Adds R function names to the list of functions, that
#' can be called from a web page without manual confirmation on the R side.
#'
#' @param funs Vector of function names to be added to the list. If \code{NULL},
#' returns names of all currently allowed R functions.
#'
#' @return Names of all currently allowed functions if \code{funs = NULL}.
#'
#' @examples
#' \donttest{openPage()
#' allowFunctions(c("myFunction1", "print", "someObject$method"))
#' funs <- allowFunctions()
#' closePage()}
#'
#' @seealso \code{\link{allowVariables}}, \code{\link{authorize}}, \code{\link{openPage}} (check argument
#' \code{allowedFunctions}), \code{\link{callFunction}}.
#'
#' @export
allowFunctions <- function(funs = NULL) {
if(is.null(pkg.env$app))
stop("There is no opened page. Please, use 'openPage()' function to create one.")
pkg.env$app$allowFunctions(funs)
}
#' Allow variable assignment without authorization
#'
#' This function adds variable names to the list of variables, that
#' can be modified from a web page without manual confirmation on the R side.
#'
#' @param vars Vector of variable names to be added to the list. If \code{NULL},
#' returns names of all currently allowed variables.
#'
#' @examples
#' \donttest{openPage()
#' allowVariables(c("myVariable", "anotherOne"))
#' vars <- allowVariables()
#' closePage()}
#'
#' @return Names of all currently allowed variables if \code{vars = NULL}.
#'
#' @seealso \code{\link{allowFunctions}}, \code{\link{authorize}}, \code{\link{openPage}} (check argument
#' \code{allowedVariables}), \code{\link{sendData}}.
#'
#' @export
allowVariables <- function(vars = NULL) {
if(is.null(pkg.env$app))
stop("There is no opened page. Please, use 'openPage()' function to create one.")
pkg.env$app$allowVariables(vars)
}
#' Change size of the message storage
#'
#' This function allows to change number or total size of the messages
#' that are received via the websocket and are stored in memory.
#'
#' For security reasons, control of the currently running R session is limited
#' to calling and changing only some user specified functions and variables. All other
#' messages are stored in memory and can be later processed
#' by calling \code{\link{authorize}} function. To prevent overuse of memory, one can
#' limit size of the storage by number of messages or by their total size estimated
#' by \code{\link[utils]{object.size}}. If the storage grows above these limits, older
#' messages are removed. The last received message will not be removed even if it
#' takes more memory than is allowed. If any of the size parameters are
#' set to zero, no massages will be stored. In this case, any message that requires authorization will be
#' automatically discarded.
#'
#' One can also directly change public fields \code{maxN} and \code{maxSize} of any object
#' of class \code{\link{Session}} (see also \code{\link{getSession}}).
#'
#' @param n Maximum number of messages that can be stored simultaneously.
#' @param size Maximum total size of all stored messages in bytes.
#' @param sessionId ID of the session, for which the storage size should be changed.
#' Can also be a vector of session IDs to change storage size for multiple sessions at once.
#' If \code{NULL}, changes will be applied to all currently active sessions.
#'
#' @examples
#' \donttest{openPage()
#' limitStorage(n = 10)
#' limitStorage(size = 10 * 1024^2)
#' closePage()}
#'
#' @seealso \code{\link{authorize}}, \code{\link{allowFunctions}}, \code{\link{allowVariables}}.
#'
#' @export
limitStorage <- function(n = NULL, size = NULL, sessionId = NULL) {
if(is.null(pkg.env$app))
stop("There is no opened page. Please, use 'openPage()' function to create one.")
if(!is.null(n)) {
if(!is.numeric(n))
stop("Maximum number of stored messages 'n' must be numeric")
if(n < 0)
stop("Maximum number of stored messages 'n' must be non-negative")
}
if(!is.null(size)) {
if(!is.numeric(size))
stop("Maximum size of stored messages 'size' must be numeric")
if(size < 0)
stop("Maximum size of stored messages 'size' must be non-negative")
}
if(is.null(sessionId))
sessionId <- getSessionIds()
stopifnot(is.character(sessionId) && is.vector(sessionId))
for(id in sessionId){
session <- getSession(id)
if(!is.null(session)) {
if(!is.null(n))
session$maxN <- n
if(!is.null(size))
session$maxSize <- size
} else {
warning(str_c("There is no session with ID ", id))
}
}
}
#' Get the currently running app
#'
#' \code{jrc} offers two ways to control an interactive app. One is by using methods of classes
#' \code{\link{App}} and \code{\link{Session}}. This allows one to have any number of apps within one
#' R session, but requires some understanding of object oriented programming. Another way is to use
#' provided wrapper functions that are exported by the package. These functions internally work with
#' the \code{\link{App}} object, which is stored in the package namespace upon initialization with
#' \code{\link{openPage}} function. \code{getPage} returns this object if any.
#'
#' @return Object of class \code{\link{App}} or \code{NULL} if there is no active app.
#'
#' @export
getPage <- function() {
pkg.env$app
}
#' Adds variables to a session environment
#'
#' Each client session in \code{jrc}, gets its own environment that can be accessed only by this
#' session (or from the outside with the \code{\link{getSessionVariable}} function). General purpose
#' of these environments is to store some session-specific information such as state of the app for
#' each user. It can also be used to mask variables from the user: if there are two variables with the
#' same name in the session environment and outside of it, user will not be able to see the latter one.
#' This function adds new variables to a session environment or changes values of some existing ones.
#'
#' This function is a wrapper around method \code{sessionVariables} of class \code{\link{Session}}.
#' If \code{makeDefault = TRUE}, it is also a wrapper around method \code{sessionVariables} of class
#' \code{\link{App}}. The first one changes the current state of the session environment, while the
#' second specifies default variables for each new session.
#'
#' @param vars Named list of variables to be added to a session environment. Names are required and
#' will be used as variable names.
#' @param sessionId ID of the session to which variables should be added. Can also be a vector of
#' multiple session IDs. If \code{NULL}, then variables will be added to all currently active sessions.
#' @param makeDefault If \code{TRUE} then, in addition, the specified variables will be added to each
#' new opened session as default ones.
#'
#' @examples
#' \donttest{openPage(allowedFunctions = "f", allowedVariables = "res")
#'
#' m <- 1
#' f <- function() {v * m}
#' setSessionVariables(list(v = 1:10, m = 2))
#'
#' sendCommand("jrc.callFunction('f', [], 'res')", wait = 1)
#' print(res)
#'
#' closePage()}
#'
#' @seealso \code{\link{getSessionVariable}}.
#'
#' @export
setSessionVariables <- function(vars, sessionId = NULL, makeDefault = FALSE) {
if(is.null(pkg.env$app))
stop("There is no opened page. Please, use 'openPage()' function to create one.")
if(makeDefault)
pkg.env$app$sessionVariables(vars)
if(is.null(sessionId))
sessionId <- getSessionIds()
stopifnot(is.vector(sessionId))
for(id in sessionId) {
session <- getSession(id)
if(!is.null(session)) {
session$sessionVariables(vars = vars)
} else {
warning(str_c("There is no session with ID "), id)
}
}
}
#' Get IDs of all active sessions
#'
#' Returns IDs of all currently active sessions. An ID is a randomly generated combination of 6 letters and
#' numbers that is assigned to each session upon opening. This function is a wrapper around method \code{getSessionIds}
#' of class \code{\link{App}}.
#'
#' @return Vector of session IDs.
#'
#' @export
getSessionIds <- function() {
if(is.null(pkg.env$app))
stop("There is no opened page. Please, use 'openPage()' function to create one.")
pkg.env$app$getSessionIds()
}
#' Close one or several client sessions
#'
#' Closes websocket connections for the selected client sessions and removes all the related
#' information from memory. If no arguments are provided and there is only one active session,
#' closes it. This function is a wrapper around method \code{closeSession} of
#' class \code{\link{App}}.
#'
#' @param sessionId IDs of the sessions to close. Can be a vector of multiple IDs.
#' @param inactive All sessions that were inactive (didn't receive any messages) for the
#' specified amount of time (in seconds) will be closed.
#' @param old All sessions that were opened for at least specified amount of time (in seconds)
#' will be closed.
#'
#' @examples
#' \donttest{start <- Sys.time()
#' openPage()
#'
#' app <- getPage()
#' time <- Sys.time()
#'
#' app$openPage(FALSE)
#' app$openPage(FALSE)
#'
#' print(getSessionIds())
#'
#' # No sessions will be closed
#' closeSession(old = Sys.time() - start)
#' print(getSessionIds())
#'
#' # One session (the one that has been opened first) will be closed
#' closeSession(old = Sys.time() - time)
#' print(getSessionIds())
#'
#' time <- Sys.time()
#' sendCommand("jrc.sendCommand('print(\"Hi!\")')", sessionId = getSessionIds()[1], wait = 3)
#'
#' closeSession(inactive = Sys.time() - time)
#' closeSession()
#'
#' closePage()}
#'
#' @export
closeSession <- function(sessionId = NULL, inactive = NULL, old = NULL) {
if(is.null(pkg.env$app))
stop("There is no opened page. Please, use 'openPage()' function to create one.")
pkg.env$app$closeSession(sessionId, inactive, old)
}
#' Get IDs of all stored messages
#'
#' Returns IDs of all currently stored messages.
#'
#' For security reasons, most of the messages that are received
#' from web pages require manual authorization in the R session with \code{\link{authorize}} function. Until that happens,
#' messages are given randomly generated IDs and are stored in memory.
#'
#' This function is a wrapper around method \code{getMessageIds} of class \code{\link{Session}}.
#'
#' @param sessionId ID of the session for which to return message IDs. Can also be a vector of multiple session IDs.
#' If \code{NULL}, returns message IDs for all currently active sessions.
#' @param simplify If \code{TRUE} and only one session ID is provided (or there is only one active session), returns
#' a vector of message IDs. Otherwise returns a named list with one vector for each requested session.
#'
#' @return Either a named list or a vector with message IDs.
#'
#' @seealso \code{\link{authorize}}, \code{\link{getSessionIds}}.
#'
#' @export
getMessageIds <- function(sessionId = NULL, simplify = TRUE) {
if(is.null(pkg.env$app))
stop("There is no opened page. Please, use 'openPage()' function to create one.")
if(is.null(sessionId))
sessionId <- getSessionIds()
stopifnot(is.vector(sessionId))
msgs <- list()
for(id in sessionId) {
session <- getSession(id)
if(!is.null(session)) {
msgs[[id]] <- session$getMessageIds()
} else {
warning("There is no session with ID ", id)
}
}
if(simplify & length(msgs) == 1)
msgs <- msgs[[1]]
msgs
}
#' Removes a stored message
#'
#' Removes a message from the storage of a session. This function is a wrapper around
#' method \code{removeMessage} of class \code{\link{Session}}.
#'
#' @param sessionId ID of the session from where to remove a message. If there is only one active session, this argument
#' becomes optional.
#' @param messageId ID of the message to remove. If there is only one stored message, this argument becomes optional.
#'
#' @seealso \code{\link{authorize}}, \code{\link{getMessageIds}}.
#'
#' @export
removeMessage <- function(sessionId = NULL, messageId = NULL) {
if(is.null(pkg.env$app))
stop("There is no opened page. Please, use 'openPage()' function to create one.")
session <- getSession(sessionId)
if(is.null(session))
stop(str_c("There is no session with ID ", sessionId))
session$removeMessage(messageId)
}
#' Get a session
#'
#' Returns \code{\link{Session}} by its ID. This function is a wrapper around method
#' \code{getSession} of class \code{\link{App}}.
#'
#' @param sessionId ID of the session. If there is only one active session, this argument becomes optional.
#'
#' @return Object of class \code{\link{Session}}.
#'
#' @export
getSession <- function(sessionId = NULL){
if(is.null(pkg.env$app))
stop("There is no opened page. Please, use 'openPage()' function to create one.")
pkg.env$app$getSession(sessionId)
}
#' Get a variable from a client session environment
#'
#' This function returns a variable, how it is seen from a session, e.g. for all the received function calls and
#' commands. It searches for the variable in the session environment first, and then, if variable is not found, checks enclosing
#' frames of the environment, starting from the outer environment of the app (see \code{\link{setEnvironment}}). If the variable
#' doesn't exist, throws an error.
#'
#' This function
#' is a wrapper around method \code{sessionVariables} of the class \code{\link{Session}}.
#'
#' @param varName Name of the variable to search for. Must be a character.
#' @param sessionId ID of the session. If there is only one active session, this argument becomes optional.
#'
#' @return Requested variable
#'
#' @examples
#' \donttest{f <- function(x) {x * 3}
#' openPage(allowedFunctions = "f", allowedVariables = "k")
#' getPage()$openPage(FALSE)
#' id1 <- getSessionIds()[1]
#' id2 <- getSessionIds()[2]
#' sendCommand("jrc.callFunction('f', [10], 'k')", sessionId = id1, wait = 3)
#' sendCommand("jrc.callFunction('f', [20], 'k')", sessionId = id2, wait = 3)
#' k1 <- getSessionVariable("k", id1)
#' k2 <- getSessionVariable("k", id2)
#'
#' closePage()}
#'
#' @seealso \code{\link{setSessionVariables}}
#'
#' @export
getSessionVariable <- function(varName, sessionId = NULL) {
if(is.null(pkg.env$app))
stop("There is no opened page. Please, use 'openPage()' function to create one.")
session <- getSession(sessionId)
if(is.null(session))
stop(str_c("There is no session with ID ", sessionId))
session$sessionVariables(varName = varName)
}
#' Remove variables from a client session environment
#'
#' This function removes variables from the environment of a client session. It allows, for instance, to unmask
#' a variable with the same name from the outer app environment (see \code{\link{setEnvironment}}) for the session
#' (check the example below). This function
#' is a wrapper around method \code{sessionVariables} of the class \code{\link{Session}}.
#'
#' @param varNames Names of variables to remove.
#' @param sessionId ID of the session. If there is only one active session, this argument becomes optional.
#'
#' @examples
#' \donttest{openPage(allowedVariables = "k", sessionVars = list(k = 10))
#'
#' k <- -1
#' getPage()$openPage(FALSE)
#' id1 <- getSessionIds()[1]
#' id2 <- getSessionIds()[2]
#' removeSessionVariables("k", id1)
#' #this changes global 'k', since the variable is no longer masked
#' sendCommand("jrc.sendData('k', 1)", sessionId = id1, wait = 3)
#' #this doesn't affect global 'k'
#' sendCommand("jrc.sendData('k', 5)", sessionId = id2, wait = 3)
#' local_k <- getSessionVariable("k", id2)
#'
#' closePage()}
#'
#' @seealso \code{\link{setSessionVariables}}
#'
#' @export
removeSessionVariables <- function(varNames, sessionId = NULL) {
if(is.null(pkg.env$app))
stop("There is no opened page. Please, use 'openPage()' function to create one.")
session <- getSession(sessionId)
if(is.null(session))
stop(str_c("There is no session with ID ", sessionId))
session$sessionVariables(remove = varNames)
} jrc/NEWS.md 0000644 0001762 0000144 00000004515 13617025403 012134 0 ustar ligges users # jrc 0.3.1
* Issue with file separator in Windows fixed.
* Now app is stored in packages namespace immediately after initialization. This fixes a problem with wrapper functions inside `onStart`.
# jrc 0.3.0
* `jrc` now supports multiple connections to a single server and thus can be used to create server apps that are intended
to be used by multiple clients simultaneously. This change requires some additional arguments in some of the functions as well
as several new ones. However, backwards compatibility is maintained.
* `jrc` now depends on `R6` and each app is represented with a single object. One can manage the app with methods of this object,
which allows to run several apps inside one R session. For more information, check man pages of classes `App` and `Session`.
# jrc 0.2.1
* `jrc` now works properly on RStudio Server
* `sendData` no longer crashes when `keepAsVector = FALSE` and some NAs are present.
* `jrc` now works with `httpuv < 1.5.2` (it no longer depends on `httpuv::randomPort()`).
* `jrc` now imports `mime` for defining content type when serving a page.
# jrc 0.2.0
* Now most of the request from the server must be manually authorized in the R session to prevent misuse of publicly available
apps based on jrc. Functions `authorize`, `allowVariables`, `allowFunctions`, `limitStorage` have been added. Check their man
pages for more information.
* `openPage` now have `browser` argument, which allows to specify a browser to open a page (previously the default browser was
used with no alternatives).
* Function `getPage` is added. This function returns the main page-handling object with all the information about current session.
* Function `callFunction` added on both R and JavaScript sides. It allows to call a function by name, list of arguments and name
of variable to which assign the result.
* Now port for the local server is selected by `httpuv::randomPort()` function. User can also provide a port number as an
argument to the `openPage` function.
* `sendData` now has argument `rowwise` which allows to send matrices and data.frames to JavaScript not only rowwise (default),
but also columnwise.
* If in `sendData` `keepAsVector = FALSE` `jrc` now checks recursively for any arrays of length 1 to replace them with scalars
(important for lists).
* Some bugs with changing variable types in `jrc.sendData` fixed. jrc/MD5 0000644 0001762 0000144 00000002676 13617246222 011360 0 ustar ligges users 13ca73d01ed23e12728bacc9e84b5502 *DESCRIPTION
9b10db9d94e5975ebbf23e551bfc9146 *NAMESPACE
145c689f1e25e46781e52e3e51618588 *NEWS.md
f29874b7271d5d3960495348a0d98b01 *R/jrc.R
90b7a0674c7e4fe3771d2015383ef140 *inst/http_root/index.html
b60be6d7e6ec004ab2dae282d0a7a170 *inst/http_root/jrc.js
eada35a7fe18b0bf9cd86acd9a174d21 *man/App.Rd
e4dbe56ce2794783432bea2e91cfb6e6 *man/Session.Rd
51ddbf072556dd164d5eedd919ed146a *man/allowFunctions.Rd
ce5db47b704468a97f7960dd4638ea69 *man/allowVariables.Rd
045c097924017149b237a00810125905 *man/authorize.Rd
e3504328b5aaf642143b4b3c0a4da961 *man/callFunction.Rd
cebb0f456641343875348536316f609a *man/closePage.Rd
3533f47e28ac786e09f8ae0e7252b1f7 *man/closeSession.Rd
35b4befdd7c509ead53d7296ab92d879 *man/getMessageIds.Rd
2a0351e4eeb63ddc4b215f5185147625 *man/getPage.Rd
62a4118cced49e9620f4ec65dcc5e90f *man/getSession.Rd
6e8e1287ae44c66288d747326fc795dc *man/getSessionIds.Rd
f03ae3d160a646c8919c0204fa09dfc8 *man/getSessionVariable.Rd
dd6d92374ef6d55c526dc36f4829279b *man/limitStorage.Rd
912f5f86fd5b93c51339e4a8582e1e4c *man/openPage.Rd
04d058597bfd10fc396ca667f42b2d65 *man/removeMessage.Rd
390f640a040dbfefe41a24b940376d8b *man/removeSessionVariables.Rd
d0d26ddc4bcaa314f8c7919a6c48cb8c *man/sendCommand.Rd
481781a204440ba86366c36a09711364 *man/sendData.Rd
921a146fb1d5645a72c7d54f0e8dec75 *man/sendHTML.Rd
2b4ef01512943616a4b887c9b725658c *man/setEnvironment.Rd
91b1554fb62ee2877012f4640cf4ae62 *man/setSessionVariables.Rd
jrc/inst/ 0000755 0001762 0000144 00000000000 13531466670 012020 5 ustar ligges users jrc/inst/http_root/ 0000755 0001762 0000144 00000000000 13531466670 014042 5 ustar ligges users jrc/inst/http_root/jrc.js 0000644 0001762 0000144 00000034505 13574227453 015166 0 ustar ligges users //get current url
var urlSpl = window.location.href.split("/"),
urlWs = "";
if(urlSpl[0] == "https:")
urlWs += "wss://"
else
urlWs += "ws://";
urlWs += urlSpl[2] + "/";
if(urlSpl[3] == "p")
urlWs += "p/" + urlSpl[4] + "/";
jrc = {};
// establish WebSocket link and handlers
jrc.ws = new WebSocket( urlWs )
jrc.ws.addEventListener( "open", function(event) {
// ...
} );
jrc.ws.addEventListener( "message", function(event) {
msg = JSON.parse( event.data );
if(msg[0] == "COM") {
eval(msg[1]);
return;
}
if(msg[0] == "DATA") {
//msg[1] - variable name
//msg[2] - variable content
//msg[3] - keepAsVector ('TRUE' or 'FALSE')
if(msg[1] == undefined || !jrc.isValidName(msg[1])) {
console.log("DATA message with invalid variable name has been recieved: " + msg[1])
ws.send("warning('Invalid variable name: " + msg[1] + "')");
return;
}
window[msg[1]] = JSON.parse(msg[2]);
// check if we also got `keepAsVector` parameter and it's false
turnToScalar = function(v) {
if(!v || (v.length === undefined && typeof v !== "object") || typeof v === "string") return v;
if(Array.isArray(v)) {
if(v.length == 1) return v[0];
for(var i = 0; i < v.length; i++)
v[i] = turnToScalar(v[i]);
} else {
var keys = Object.keys(v);
for(var i = 0; i < keys.length; i++)
v[keys[i]] = turnToScalar(v[keys[i]]);
}
return v;
}
if(msg[3] == "FALSE") {
window[msg[1]] = turnToScalar(window[msg[1]]);
}
if(window[msg[1]].length && window[msg[1]][0]._row) {
var converted = {}, rowname;
for(var i = 0; i < window[msg[1]].length; i++) {
rowname = window[msg[1]][i]._row;
delete window[msg[1]][i]._row;
converted[rowname] = window[msg[1]][i];
}
window[msg[1]] = converted;
}
return;
}
if(msg[0] == "HTML") {
document.body.innerHTML += msg[1];
return;
}
if(msg[0] == "FUN") {
//msg[1] - function name
//msg[2] - variable name
//msg[3] - thisArg
//___args___ - arguments
var fCall = msg[1].split("."),
obj = window;
for(var i = 0; i < fCall.length; i++)
obj = obj[fCall[i]];
self = window;
if(msg[3])
self = window[msg[3]];
var ___tmp___ = obj.apply(self, window["___args___"]);
if(msg[2])
window[msg[2]] = ___tmp___;
window["___args___"] = null;
return;
}
if(msg[0] == "ID") {
jrc.id = msg[1]
return;
}
console.log("Unknown message type: " + msg[0])
jrc.ws.send("warning('Unknown message type: " + msg[0] + "')");
} );
jrc.ws.addEventListener( "close", function(event) {
window.close()
} );
jrc.sendCommand = function(command) {
jrc.ws.send(JSON.stringify(["COM", command]));
}
jrc.sendData = function(variableName, variable, internal) {
jrc.ws.send(JSON.stringify(["DATA", variableName, JSON.stringify(variable), internal]));
}
//args must be object (to be converted to names list in R)
jrc.callFunction = function(functionName, args, assingTo, package, internal) {
jrc.ws.send(JSON.stringify(["FUN", functionName, JSON.stringify(args), assingTo, package, internal]));
}
jrc.notifyStorage = function(id) {
alert("Your command has been stored. To execute it, please, run the 'authorize' function as it is shown in your R sesion. " +
"Use functions 'allowVariables' and 'allowFunctions' to permit automatic execution of " +
"your commands.")
}
//from https://mothereff.in/js-variables
jrc.isValidName = function(name) {
return name.search(/^(?!(?:do|if|in|for|let|new|try|var|case|else|enum|eval|false|null|this|true|void|with|break|catch|class|const|super|throw|while|yield|delete|export|import|public|return|static|switch|typeof|default|extends|finally|package|private|continue|debugger|function|arguments|interface|protected|implements|instanceof)$)[$A-Z\_a-z\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0370-\u0374\u0376\u0377\u037a-\u037d\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u048a-\u0527\u0531-\u0556\u0559\u0561-\u0587\u05d0-\u05ea\u05f0-\u05f2\u0620-\u064a\u066e\u066f\u0671-\u06d3\u06d5\u06e5\u06e6\u06ee\u06ef\u06fa-\u06fc\u06ff\u0710\u0712-\u072f\u074d-\u07a5\u07b1\u07ca-\u07ea\u07f4\u07f5\u07fa\u0800-\u0815\u081a\u0824\u0828\u0840-\u0858\u08a0\u08a2-\u08ac\u0904-\u0939\u093d\u0950\u0958-\u0961\u0971-\u0977\u0979-\u097f\u0985-\u098c\u098f\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bd\u09ce\u09dc\u09dd\u09df-\u09e1\u09f0\u09f1\u0a05-\u0a0a\u0a0f\u0a10\u0a13-\u0a28\u0a2a-\u0a30\u0a32\u0a33\u0a35\u0a36\u0a38\u0a39\u0a59-\u0a5c\u0a5e\u0a72-\u0a74\u0a85-\u0a8d\u0a8f-\u0a91\u0a93-\u0aa8\u0aaa-\u0ab0\u0ab2\u0ab3\u0ab5-\u0ab9\u0abd\u0ad0\u0ae0\u0ae1\u0b05-\u0b0c\u0b0f\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32\u0b33\u0b35-\u0b39\u0b3d\u0b5c\u0b5d\u0b5f-\u0b61\u0b71\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99\u0b9a\u0b9c\u0b9e\u0b9f\u0ba3\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bd0\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c33\u0c35-\u0c39\u0c3d\u0c58\u0c59\u0c60\u0c61\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\u0cde\u0ce0\u0ce1\u0cf1\u0cf2\u0d05-\u0d0c\u0d0e-\u0d10\u0d12-\u0d3a\u0d3d\u0d4e\u0d60\u0d61\u0d7a-\u0d7f\u0d85-\u0d96\u0d9a-\u0db1\u0db3-\u0dbb\u0dbd\u0dc0-\u0dc6\u0e01-\u0e30\u0e32\u0e33\u0e40-\u0e46\u0e81\u0e82\u0e84\u0e87\u0e88\u0e8a\u0e8d\u0e94-\u0e97\u0e99-\u0e9f\u0ea1-\u0ea3\u0ea5\u0ea7\u0eaa\u0eab\u0ead-\u0eb0\u0eb2\u0eb3\u0ebd\u0ec0-\u0ec4\u0ec6\u0edc-\u0edf\u0f00\u0f40-\u0f47\u0f49-\u0f6c\u0f88-\u0f8c\u1000-\u102a\u103f\u1050-\u1055\u105a-\u105d\u1061\u1065\u1066\u106e-\u1070\u1075-\u1081\u108e\u10a0-\u10c5\u10c7\u10cd\u10d0-\u10fa\u10fc-\u1248\u124a-\u124d\u1250-\u1256\u1258\u125a-\u125d\u1260-\u1288\u128a-\u128d\u1290-\u12b0\u12b2-\u12b5\u12b8-\u12be\u12c0\u12c2-\u12c5\u12c8-\u12d6\u12d8-\u1310\u1312-\u1315\u1318-\u135a\u1380-\u138f\u13a0-\u13f4\u1401-\u166c\u166f-\u167f\u1681-\u169a\u16a0-\u16ea\u16ee-\u16f0\u1700-\u170c\u170e-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17d7\u17dc\u1820-\u1877\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191c\u1950-\u196d\u1970-\u1974\u1980-\u19ab\u19c1-\u19c7\u1a00-\u1a16\u1a20-\u1a54\u1aa7\u1b05-\u1b33\u1b45-\u1b4b\u1b83-\u1ba0\u1bae\u1baf\u1bba-\u1be5\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c7d\u1ce9-\u1cec\u1cee-\u1cf1\u1cf5\u1cf6\u1d00-\u1dbf\u1e00-\u1f15\u1f18-\u1f1d\u1f20-\u1f45\u1f48-\u1f4d\u1f50-\u1f57\u1f59\u1f5b\u1f5d\u1f5f-\u1f7d\u1f80-\u1fb4\u1fb6-\u1fbc\u1fbe\u1fc2-\u1fc4\u1fc6-\u1fcc\u1fd0-\u1fd3\u1fd6-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ff4\u1ff6-\u1ffc\u2071\u207f\u2090-\u209c\u2102\u2107\u210a-\u2113\u2115\u2119-\u211d\u2124\u2126\u2128\u212a-\u212d\u212f-\u2139\u213c-\u213f\u2145-\u2149\u214e\u2160-\u2188\u2c00-\u2c2e\u2c30-\u2c5e\u2c60-\u2ce4\u2ceb-\u2cee\u2cf2\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d80-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u2e2f\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303c\u3041-\u3096\u309d-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3105-\u312d\u3131-\u318e\u31a0-\u31ba\u31f0-\u31ff\u3400-\u4db5\u4e00-\u9fcc\ua000-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua697\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua78e\ua790-\ua793\ua7a0-\ua7aa\ua7f8-\ua801\ua803-\ua805\ua807-\ua80a\ua80c-\ua822\ua840-\ua873\ua882-\ua8b3\ua8f2-\ua8f7\ua8fb\ua90a-\ua925\ua930-\ua946\ua960-\ua97c\ua984-\ua9b2\ua9cf\uaa00-\uaa28\uaa40-\uaa42\uaa44-\uaa4b\uaa60-\uaa76\uaa7a\uaa80-\uaaaf\uaab1\uaab5\uaab6\uaab9-\uaabd\uaac0\uaac2\uaadb-\uaadd\uaae0-\uaaea\uaaf2-\uaaf4\uab01-\uab06\uab09-\uab0e\uab11-\uab16\uab20-\uab26\uab28-\uab2e\uabc0-\uabe2\uac00-\ud7a3\ud7b0-\ud7c6\ud7cb-\ud7fb\uf900-\ufa6d\ufa70-\ufad9\ufb00-\ufb06\ufb13-\ufb17\ufb1d\ufb1f-\ufb28\ufb2a-\ufb36\ufb38-\ufb3c\ufb3e\ufb40\ufb41\ufb43\ufb44\ufb46-\ufbb1\ufbd3-\ufd3d\ufd50-\ufd8f\ufd92-\ufdc7\ufdf0-\ufdfb\ufe70-\ufe74\ufe76-\ufefc\uff21-\uff3a\uff41-\uff5a\uff66-\uffbe\uffc2-\uffc7\uffca-\uffcf\uffd2-\uffd7\uffda-\uffdc][$A-Z\_a-z\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0370-\u0374\u0376\u0377\u037a-\u037d\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u048a-\u0527\u0531-\u0556\u0559\u0561-\u0587\u05d0-\u05ea\u05f0-\u05f2\u0620-\u064a\u066e\u066f\u0671-\u06d3\u06d5\u06e5\u06e6\u06ee\u06ef\u06fa-\u06fc\u06ff\u0710\u0712-\u072f\u074d-\u07a5\u07b1\u07ca-\u07ea\u07f4\u07f5\u07fa\u0800-\u0815\u081a\u0824\u0828\u0840-\u0858\u08a0\u08a2-\u08ac\u0904-\u0939\u093d\u0950\u0958-\u0961\u0971-\u0977\u0979-\u097f\u0985-\u098c\u098f\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bd\u09ce\u09dc\u09dd\u09df-\u09e1\u09f0\u09f1\u0a05-\u0a0a\u0a0f\u0a10\u0a13-\u0a28\u0a2a-\u0a30\u0a32\u0a33\u0a35\u0a36\u0a38\u0a39\u0a59-\u0a5c\u0a5e\u0a72-\u0a74\u0a85-\u0a8d\u0a8f-\u0a91\u0a93-\u0aa8\u0aaa-\u0ab0\u0ab2\u0ab3\u0ab5-\u0ab9\u0abd\u0ad0\u0ae0\u0ae1\u0b05-\u0b0c\u0b0f\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32\u0b33\u0b35-\u0b39\u0b3d\u0b5c\u0b5d\u0b5f-\u0b61\u0b71\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99\u0b9a\u0b9c\u0b9e\u0b9f\u0ba3\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bd0\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c33\u0c35-\u0c39\u0c3d\u0c58\u0c59\u0c60\u0c61\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\u0cde\u0ce0\u0ce1\u0cf1\u0cf2\u0d05-\u0d0c\u0d0e-\u0d10\u0d12-\u0d3a\u0d3d\u0d4e\u0d60\u0d61\u0d7a-\u0d7f\u0d85-\u0d96\u0d9a-\u0db1\u0db3-\u0dbb\u0dbd\u0dc0-\u0dc6\u0e01-\u0e30\u0e32\u0e33\u0e40-\u0e46\u0e81\u0e82\u0e84\u0e87\u0e88\u0e8a\u0e8d\u0e94-\u0e97\u0e99-\u0e9f\u0ea1-\u0ea3\u0ea5\u0ea7\u0eaa\u0eab\u0ead-\u0eb0\u0eb2\u0eb3\u0ebd\u0ec0-\u0ec4\u0ec6\u0edc-\u0edf\u0f00\u0f40-\u0f47\u0f49-\u0f6c\u0f88-\u0f8c\u1000-\u102a\u103f\u1050-\u1055\u105a-\u105d\u1061\u1065\u1066\u106e-\u1070\u1075-\u1081\u108e\u10a0-\u10c5\u10c7\u10cd\u10d0-\u10fa\u10fc-\u1248\u124a-\u124d\u1250-\u1256\u1258\u125a-\u125d\u1260-\u1288\u128a-\u128d\u1290-\u12b0\u12b2-\u12b5\u12b8-\u12be\u12c0\u12c2-\u12c5\u12c8-\u12d6\u12d8-\u1310\u1312-\u1315\u1318-\u135a\u1380-\u138f\u13a0-\u13f4\u1401-\u166c\u166f-\u167f\u1681-\u169a\u16a0-\u16ea\u16ee-\u16f0\u1700-\u170c\u170e-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17d7\u17dc\u1820-\u1877\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191c\u1950-\u196d\u1970-\u1974\u1980-\u19ab\u19c1-\u19c7\u1a00-\u1a16\u1a20-\u1a54\u1aa7\u1b05-\u1b33\u1b45-\u1b4b\u1b83-\u1ba0\u1bae\u1baf\u1bba-\u1be5\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c7d\u1ce9-\u1cec\u1cee-\u1cf1\u1cf5\u1cf6\u1d00-\u1dbf\u1e00-\u1f15\u1f18-\u1f1d\u1f20-\u1f45\u1f48-\u1f4d\u1f50-\u1f57\u1f59\u1f5b\u1f5d\u1f5f-\u1f7d\u1f80-\u1fb4\u1fb6-\u1fbc\u1fbe\u1fc2-\u1fc4\u1fc6-\u1fcc\u1fd0-\u1fd3\u1fd6-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ff4\u1ff6-\u1ffc\u2071\u207f\u2090-\u209c\u2102\u2107\u210a-\u2113\u2115\u2119-\u211d\u2124\u2126\u2128\u212a-\u212d\u212f-\u2139\u213c-\u213f\u2145-\u2149\u214e\u2160-\u2188\u2c00-\u2c2e\u2c30-\u2c5e\u2c60-\u2ce4\u2ceb-\u2cee\u2cf2\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d80-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u2e2f\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303c\u3041-\u3096\u309d-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3105-\u312d\u3131-\u318e\u31a0-\u31ba\u31f0-\u31ff\u3400-\u4db5\u4e00-\u9fcc\ua000-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua697\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua78e\ua790-\ua793\ua7a0-\ua7aa\ua7f8-\ua801\ua803-\ua805\ua807-\ua80a\ua80c-\ua822\ua840-\ua873\ua882-\ua8b3\ua8f2-\ua8f7\ua8fb\ua90a-\ua925\ua930-\ua946\ua960-\ua97c\ua984-\ua9b2\ua9cf\uaa00-\uaa28\uaa40-\uaa42\uaa44-\uaa4b\uaa60-\uaa76\uaa7a\uaa80-\uaaaf\uaab1\uaab5\uaab6\uaab9-\uaabd\uaac0\uaac2\uaadb-\uaadd\uaae0-\uaaea\uaaf2-\uaaf4\uab01-\uab06\uab09-\uab0e\uab11-\uab16\uab20-\uab26\uab28-\uab2e\uabc0-\uabe2\uac00-\ud7a3\ud7b0-\ud7c6\ud7cb-\ud7fb\uf900-\ufa6d\ufa70-\ufad9\ufb00-\ufb06\ufb13-\ufb17\ufb1d\ufb1f-\ufb28\ufb2a-\ufb36\ufb38-\ufb3c\ufb3e\ufb40\ufb41\ufb43\ufb44\ufb46-\ufbb1\ufbd3-\ufd3d\ufd50-\ufd8f\ufd92-\ufdc7\ufdf0-\ufdfb\ufe70-\ufe74\ufe76-\ufefc\uff21-\uff3a\uff41-\uff5a\uff66-\uffbe\uffc2-\uffc7\uffca-\uffcf\uffd2-\uffd7\uffda-\uffdc0-9\u0300-\u036f\u0483-\u0487\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u064b-\u0669\u0670\u06d6-\u06dc\u06df-\u06e4\u06e7\u06e8\u06ea-\u06ed\u06f0-\u06f9\u0711\u0730-\u074a\u07a6-\u07b0\u07c0-\u07c9\u07eb-\u07f3\u0816-\u0819\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0859-\u085b\u08e4-\u08fe\u0900-\u0903\u093a-\u093c\u093e-\u094f\u0951-\u0957\u0962\u0963\u0966-\u096f\u0981-\u0983\u09bc\u09be-\u09c4\u09c7\u09c8\u09cb-\u09cd\u09d7\u09e2\u09e3\u09e6-\u09ef\u0a01-\u0a03\u0a3c\u0a3e-\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a66-\u0a71\u0a75\u0a81-\u0a83\u0abc\u0abe-\u0ac5\u0ac7-\u0ac9\u0acb-\u0acd\u0ae2\u0ae3\u0ae6-\u0aef\u0b01-\u0b03\u0b3c\u0b3e-\u0b44\u0b47\u0b48\u0b4b-\u0b4d\u0b56\u0b57\u0b62\u0b63\u0b66-\u0b6f\u0b82\u0bbe-\u0bc2\u0bc6-\u0bc8\u0bca-\u0bcd\u0bd7\u0be6-\u0bef\u0c01-\u0c03\u0c3e-\u0c44\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62\u0c63\u0c66-\u0c6f\u0c82\u0c83\u0cbc\u0cbe-\u0cc4\u0cc6-\u0cc8\u0cca-\u0ccd\u0cd5\u0cd6\u0ce2\u0ce3\u0ce6-\u0cef\u0d02\u0d03\u0d3e-\u0d44\u0d46-\u0d48\u0d4a-\u0d4d\u0d57\u0d62\u0d63\u0d66-\u0d6f\u0d82\u0d83\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0df2\u0df3\u0e31\u0e34-\u0e3a\u0e47-\u0e4e\u0e50-\u0e59\u0eb1\u0eb4-\u0eb9\u0ebb\u0ebc\u0ec8-\u0ecd\u0ed0-\u0ed9\u0f18\u0f19\u0f20-\u0f29\u0f35\u0f37\u0f39\u0f3e\u0f3f\u0f71-\u0f84\u0f86\u0f87\u0f8d-\u0f97\u0f99-\u0fbc\u0fc6\u102b-\u103e\u1040-\u1049\u1056-\u1059\u105e-\u1060\u1062-\u1064\u1067-\u106d\u1071-\u1074\u1082-\u108d\u108f-\u109d\u135d-\u135f\u1712-\u1714\u1732-\u1734\u1752\u1753\u1772\u1773\u17b4-\u17d3\u17dd\u17e0-\u17e9\u180b-\u180d\u1810-\u1819\u18a9\u1920-\u192b\u1930-\u193b\u1946-\u194f\u19b0-\u19c0\u19c8\u19c9\u19d0-\u19d9\u1a17-\u1a1b\u1a55-\u1a5e\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1b00-\u1b04\u1b34-\u1b44\u1b50-\u1b59\u1b6b-\u1b73\u1b80-\u1b82\u1ba1-\u1bad\u1bb0-\u1bb9\u1be6-\u1bf3\u1c24-\u1c37\u1c40-\u1c49\u1c50-\u1c59\u1cd0-\u1cd2\u1cd4-\u1ce8\u1ced\u1cf2-\u1cf4\u1dc0-\u1de6\u1dfc-\u1dff\u200c\u200d\u203f\u2040\u2054\u20d0-\u20dc\u20e1\u20e5-\u20f0\u2cef-\u2cf1\u2d7f\u2de0-\u2dff\u302a-\u302f\u3099\u309a\ua620-\ua629\ua66f\ua674-\ua67d\ua69f\ua6f0\ua6f1\ua802\ua806\ua80b\ua823-\ua827\ua880\ua881\ua8b4-\ua8c4\ua8d0-\ua8d9\ua8e0-\ua8f1\ua900-\ua909\ua926-\ua92d\ua947-\ua953\ua980-\ua983\ua9b3-\ua9c0\ua9d0-\ua9d9\uaa29-\uaa36\uaa43\uaa4c\uaa4d\uaa50-\uaa59\uaa7b\uaab0\uaab2-\uaab4\uaab7\uaab8\uaabe\uaabf\uaac1\uaaeb-\uaaef\uaaf5\uaaf6\uabe3-\uabea\uabec\uabed\uabf0-\uabf9\ufb1e\ufe00-\ufe0f\ufe20-\ufe26\ufe33\ufe34\ufe4d-\ufe4f\uff10-\uff19\uff3f]*$/) + 1;
}
jrc/inst/http_root/index.html 0000644 0001762 0000144 00000000120 13531466670 016030 0 ustar ligges users
New jrc page