js: R to JavaScript translation

View source: R/js.R

jsR Documentation

R to JavaScript translation

Description

This function is a wrapper for id similar to qp that uses eval(substitute(alist(...))) to preserve the value of ... as-is to be able to both keep operators like ">=" or "!=" unevaluated in the resulting output, as well as translating if/else clauses from R to JavaScript.

Usage

js(
  ...,
  level = 2,
  indent.by = rk.get.indent(),
  linebreaks = TRUE,
  empty.e = rk.get.empty.e(),
  keep.ite = FALSE
)

Arguments

...

One or several character strings and/or XiMpLe.node objects with plugin nodes, and/or objects of classes rk.JS.arr or rk.JS.opt, simply separated by comma. JavaScript operators and if conditions will be kept as-is.

level

Integer value, first indetation level.

indent.by

A character string defining the indentation string to use.

linebreaks

Logical, should there be line breaks between the elements in this call?

empty.e

For if conditions only: Logical, if TRUE will force to add empty else {} brackets when there is no else statement defined, which is considered to enhance code readability by some.

keep.ite

Logical, if TRUE returns if/else conditions in a list of objects of class rk.JS.ite instead of a pasted character string. Comes in handy if used inside rk.JS.options.

Details

Normally, id would simply evaluate the condition and then return the result of that evaluation, which most of the time is not what you want. With this function, you can test conditions in usual R syntax, yet the operators and if/else clauses will end up pasted in the result.

The following operators are supported: +, -, *, /, ==, !=, >, <, >=, <=, || and &&

These are currently unsupported and still need to be quoted: %, ++, –, =, +=, -=, *=, /=, %=, === and !==

Value

A character string (or rk.JS.ite, if keep.ite=TRUE and input is an if/else condition).

Previews

The current approach to toggle code parts for previews on and off is to query the value of the JavaScript variable is_preview. Only if it is false or unset the code is executed. You can construct code working this way by nesting it inside js(if("!is_preview"){<your code>}). Note that you must not place the exclamation mark before the quotes, but inside.

Note

You should nest your plugin script inside rk.local if you're making use of js(), to be sure it can find all defined objects.

See Also

rk.local, rk.JS.vars, rk.JS.array, rk.JS.options, echo, id, and the Introduction to Writing Plugins for RKWard

Examples

# an example checkbox XML node
cbox1 <- rk.XML.cbox(label="foo", value="foo1", id.name="CheckboxFoo.ID")
cat(rk.paste.JS(js(
  if(cbox1 == "foo1") {
    echo("gotcha!")
  } else {
    echo("nothing!")
  }
)))

rkward-community/rkwarddev documentation built on May 9, 2022, 3:02 p.m.