jsTranslate: Translate R code to JavaScript

Description Usage Arguments Value Note Author(s) References See Also Examples

Description

This function takes an R language object or a file containing R code and attempts to convert it to equivalent JavaScript code. It provides ways to merge R objects into the code (via the substitute parameter).

Usage

1
jsTranslate(code, substitute = list(), jsGlobals = character(), addSemiColon = NA, ...)

Arguments

code

either a language object (e.g. an expression, call, if statement, etc.) or the name of file containing R code.

substitute

a list of R objects that can be inlined within the generated JavaScript code. These are serialized to JSON format and any reference to these values in the code causes this JSON representation to be substituted.

jsGlobals

the names of variables that are to be considered global in the JavaScript code.

addSemiColon

currently ignored.

...

for methods to add parameters that they understand.

Value

A string giving the JavaScript code. This is of class "JavaScriptCode". We may introduce additional sub-classes, e.g. JavaScriptFunction

Note

We might do more work to identify the global variables and also to identify where JavaScript variables should be declared, i.e. where to place the "var" declaration.

Author(s)

Duncan Temple Lang

References

JavaScript: The Definitive Guide, David Flanagan, O'Reilly \& Associates, Inc.

See Also

See the jsRewrite function and its methods for how the translation is done.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
 jsTranslate(quote(if(is(x, "numeric")) x))

 jsTranslate(quote( x <- x + 1))

   # Rewrite a function.
 f = system.file("sampleCode", "function.R", package = "RJavaScript")
 jsTranslate(f)

   # Replace references to R variables.
 f = system.file("sampleCode", "assign.R", package = "RJavaScript")
 jsTranslate(f, list(init = c(1, 2, 3)))
 jsTranslate(f, list(init = c(1, 2, 3), w = list(abc = 1, xyz = "foo")))

omegahat/RJavaScript documentation built on May 24, 2019, 1:54 p.m.