Embedded JavaScript Engine for R
An R interface to Google’s open source JavaScript engine. This package can now be compiled either with V8 version 6+ (LTS) from nodejs or with the legacy 3.14/3.15 version of V8.
About the R package:
Binary packages for OS-X or Windows can be installed directly from CRAN:
install.packages("V8")
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:
Ubuntu versions before 19.04 ship with a rather old V8 engine in libv8-dev. The R package can be compiled against this, but the engine only supports ES5, so some “modern” JavaScript syntax may not work. A lot of JS libraries these days require this.
A recent version of the V8 engine is available in libnode-dev
from our the cran/v8 PPA:
# Ubuntu Xenial (16.04) and Bionic (18.04) only
sudo add-apt-repository ppa:cran/v8
sudo apt-get update
sudo apt-get install libnode-dev
After installing libnode-dev
you need to reinstall the R package, and you should be good to go.
The above PPA is enabled by default in Travis for R 3.5 and up. You can use the following configuration to check R packges that depend on V8:
You should delete the travis repository package cache if you switch from libv8-dev
to libnode-dev
.
The .travis.yml in the V8 repository shows other possible configurations.
On OS-X use v8 from Homebrew:
On other systems you might need to install libv8 from source.
# 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