MyNixOS website logo
Description

Interface for the 'QuickJS-NG' Lightweight 'JavaScript' Engine.

An 'R' interface to the 'QuickJS' portable 'JavaScript' engine. The engine and all 'R' to 'JavaScript' interoperability is bundled within the package, requiring no dependencies beyond a 'C' compiler.

QuickJSR

R-CMD-check CRANstatus Downloads QuickJSR statusbadge

A portable, lightweight, zero-dependency JavaScript engine for R, using QuickJS.

Values and objects are directly passed between R and QuickJS, with no need for serialization or deserialization. This both reduces overhead and allows for more complex data structures to be passed between R and JavaScript - including functions.

Installation

You can install the development version of QuickJSR from GitHub with:

# install.packages("remotes")
remotes::install_github("andrjohns/QuickJSR")

Or you can install pre-built binaries from R-Universe:

install.packages("QuickJSR", repos = c("https://andrjohns.r-universe.dev",
                                        "https://cran.r-project.org"))

Usage

For standalone or simple JavaScript code, you can use the qjs_eval() function:

library(QuickJSR)

qjs_eval("1 + 1")
#> [1] 2
qjs_eval("Math.random()")
#> [1] 0.5193045

For more complex interactions, you can create a QuickJS context and evaluate code within that context:

ctx <- JSContext$new()

Use the $source() method to load JavaScript code into the context:

# Code can be provided as a string
ctx$source(code = "function add(a, b) { return a + b; }")

# Or read from a file
subtract_js <- tempfile(fileext = ".js")
writeLines("function subtract(a, b) { return a - b; }", subtract_js)
ctx$source(file = subtract_js)

Then use the $call() method to call a specified function with arguments:

ctx$call("add", 1, 2)
#> [1] 3
ctx$call("subtract", 5, 3)
#> [1] 2

Interacting with R objects, environments, and functions

As QuickJSR uses the respective C APIs of R and QuickJS to pass values between the two, this allows for more complex data structures to be passed between R and JavaScript.

For example, you can also pass R functions to be evaluated using JavaScript arguments:

ctx$source(code = "function callRFunction(f, x, y) { return f(x, y); }")
ctx$call("callRFunction", function(x, y) x + y, 1, 2)
#> [1] 3
ctx$call("callRFunction", function(x, y) paste0(x, ",", y), "a", "b")
#> [1] "a,b"

You can pass R environments to JavaScript, and both access and update their contents:

env <- new.env()
env$x <- 1
env$y <- 2

ctx$source(code = "function accessEnv(env) { return env.x + env.y; }")
ctx$call("accessEnv", env)
#> [1] 3

ctx$source(code = "function updateEnv(env) { env.z = env.x * env.y; return env.z;}")
ctx$call("updateEnv", env)
#> [1] 2

env$z
#> [1] 2

QuickJSR also provides a global R object, which you can use to access objects and functions from various R packages:

qjs_eval('R.package("base")["Sys.Date"]()')
#> [1] "2024-06-07 03:00:00 EEST"
Metadata

Version

1.8.0

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows