Embedded JavaScript and WebAssembly Engine for R
An R interface to V8: Google’s open source JavaScript and WebAssembly engine. This package can be compiled either with V8 version 6 and up or NodeJS when built as a shared library.
Getting started
About the R package:
- Vignette: Introduction to V8 for R
- Vignette: Using NPM packages in V8 with browserify
To see some quick examples in R run:
Installation
Binary packages for OS-X or Windows can be installed directly from CRAN:
install.packages("V8")On Linux you need a suitable libv8 installation, see below.
Linux: Static libv8
On amd64/arm64 Linux/MacOS platforms it is possible (and recommended) to automatically download a suitable static build of libv8 during package installation. This is enabled by default on Ubuntu, RHEL, OpenSuse, Alpine, and MacOS. For other systems you can opt-in by setting an environment variable DOWNLOAD_STATIC_LIBV8 during installation, for example:
Sys.setenv(DOWNLOAD_STATIC_LIBV8 = 1)
install.packages("V8")This way, you can install the R package on any x64 Linux system, without external system requirements. Alternatively, it is also still possible to install libv8 from your distribution as described below. This may be needed for servers running other architectures, or when building the R package without internet access.
Debian / Ubuntu
Installation from source on Linux requires libv8. On Ubuntu / Debian you need to install either libv8-dev, or libnode-dev. On the latest systems, libv8-dev is actually an alias for libnode-dev so they are the same:
Fedora / Redhat
On Fedora we need v8-devel (which Fedora provides as part of nodejs):
On CentOS 7 / RHEL 7 we install first need to enable EPEL:
On RockyLinux 8 / RHEL 8, v8-devel can be installed from the nodejs:16-epel module repository:
Hello World
# Create a new context
library(V8)
ctx <- v8()
# Evaluate some code
ctx$eval("var foo = 123")
ctx$eval("var bar = 456")
ctx$eval("foo+bar")
# Assign / get objects
ctx$assign("foo", JS("function(x){return x*x}"))
ctx$assign("bar", JS("foo(9)"))
ctx$get("bar")Call functions from JavaScript libraries