Skip to content

Commit

Permalink
Render vignettes
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroen committed Jan 29, 2016
1 parent b1fe134 commit f9f2403
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 151 deletions.
2 changes: 1 addition & 1 deletion inst/doc/npm.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## ----, echo = FALSE, message = FALSE-------------------------------------
## ---- echo = FALSE, message = FALSE--------------------------------------
knitr::opts_chunk$set(comment = "")
library(V8)

Expand Down
75 changes: 47 additions & 28 deletions inst/doc/npm.html

Large diffs are not rendered by default.

24 changes: 14 additions & 10 deletions inst/doc/v8_intro.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
## ----, echo = FALSE, message = FALSE-------------------------------------
## ---- echo = FALSE, message = FALSE--------------------------------------
knitr::opts_chunk$set(comment = "")
library(V8)

## ------------------------------------------------------------------------
# Create a new context
ct <- new_context();
ct <- v8()

# Evaluate some code
ct$eval("var foo = 123")
Expand All @@ -18,7 +18,7 @@ cat(ct$eval("JSON.stringify({x:Math.random()})"))
# Simple closure
ct$eval("(function(x){return x+1;})(123)")

## ----, eval=FALSE--------------------------------------------------------
## ---- eval=FALSE---------------------------------------------------------
# ct$source(system.file("js/underscore.js", package="V8"))
# ct$source("https://cdnjs.cloudflare.com/ajax/libs/crossfilter/1.3.11/crossfilter.min.js")

Expand All @@ -38,31 +38,31 @@ ct$get("bar")
## ------------------------------------------------------------------------
ct$call("_.filter", mtcars, JS("function(x){return x.mpg < 15}"))

## ----, eval=FALSE--------------------------------------------------------
## ---- eval=FALSE---------------------------------------------------------
# # Load some data
# data(diamonds, package = "ggplot2")
# ct$assign("diamonds", diamonds)
# ct$console()

## ----, eval=FALSE--------------------------------------------------------
## ---- eval=FALSE---------------------------------------------------------
# output <- ct$get("output")
# print(output)

## ----, eval=FALSE--------------------------------------------------------
# ct <- new_context()
## ---- eval=FALSE---------------------------------------------------------
# ct <- v8()
# ct$source("https://cdnjs.cloudflare.com/ajax/libs/crossfilter/1.3.11/crossfilter.min.js")
# ct$eval('var cf = crossfilter || console.error("failed to load crossfilter!")')

## ------------------------------------------------------------------------
ct <- new_context(typed_arrays = FALSE);
ct <- v8(typed_arrays = FALSE);
ct$get(JS("Object.keys(global)"))

## ------------------------------------------------------------------------
ct <- new_context(typed_arrays = TRUE);
ct <- v8(typed_arrays = TRUE);
ct$get(JS("Object.keys(global)"))

## ------------------------------------------------------------------------
ct2 <- new_context(global = NULL, console = FALSE)
ct2 <- v8(global = NULL, console = FALSE)
ct2$get(JS("Object.keys(this).length"))
ct2$assign("cars", cars)
ct2$eval("var foo = 123")
Expand All @@ -86,3 +86,7 @@ ct$validate("function(x){2*x}")
ct$validate("(function(x){2*x})")
ct$validate("!function(x){2*x}")

## ---- eval=FALSE---------------------------------------------------------
# ctx <- v8()
# ctx$console()

58 changes: 48 additions & 10 deletions inst/doc/v8_intro.Rmd
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
---
title: "Introduction to V8 for R"
author: "Jeroen Ooms"
date: "`r Sys.Date()`"
output:
html_document:
highlight : "kate"
vignette: >
%\VignetteEngine{knitr::rmarkdown}
%\VignetteIndexEntry{Introduction to V8 for R}
\usepackage[utf8]{inputenc}
output:
knitr:::html_vignette:
toc: yes
---

```{r, echo = FALSE, message = FALSE}
knitr::opts_chunk$set(comment = "")
library(V8)
```

## What is V8

V8 is Google’s open source, high performance JavaScript engine. It is written in C++ and implements ECMAScript as specified in ECMA-262, 5th edition. The V8 R package builds on the C++ library to provide a completely standalone JavaScript engine within R:

```{r}
# Create a new context
ct <- new_context();
ct <- v8()
# Evaluate some code
ct$eval("var foo = 123")
Expand Down Expand Up @@ -139,7 +137,7 @@ ct$eval('console.error("Oh no! An error!")')
A example of using `console.error` is to verify that external resources were loaded:

```{r, eval=FALSE}
ct <- new_context()
ct <- v8()
ct$source("https://cdnjs.cloudflare.com/ajax/libs/crossfilter/1.3.11/crossfilter.min.js")
ct$eval('var cf = crossfilter || console.error("failed to load crossfilter!")')
```
Expand All @@ -149,21 +147,21 @@ ct$eval('var cf = crossfilter || console.error("failed to load crossfilter!")')
Unlike what you might be used to from Node or your browser, the global namespace for a new context if very minimal. By default it contains only a few objects: `global` (a refence to itself), `console` (for `console.log` and friends) and `print` (an alias of console.log needed by some JavaScript libraries)

```{r}
ct <- new_context(typed_arrays = FALSE);
ct <- v8(typed_arrays = FALSE);
ct$get(JS("Object.keys(global)"))
```

If typed arrays are enabled it contains some additional functions:

```{r}
ct <- new_context(typed_arrays = TRUE);
ct <- v8(typed_arrays = TRUE);
ct$get(JS("Object.keys(global)"))
```

A context always has a global scope, even when no name is set. When a context is initiated with `global = NULL`, it can still be reached by evaluating the `this` keyword within the global scope:

```{r}
ct2 <- new_context(global = NULL, console = FALSE)
ct2 <- v8(global = NULL, console = FALSE)
ct2$get(JS("Object.keys(this).length"))
ct2$assign("cars", cars)
ct2$eval("var foo = 123")
Expand Down Expand Up @@ -200,3 +198,43 @@ To check if an anonymous function is syntactically valid, prefix it with `!` or
ct$validate("(function(x){2*x})")
ct$validate("!function(x){2*x}")
```


## Callback To R

A recently added feature is to interact with R from within JavaScript using the `console.r` API`. This is most easily demonstrated via the interactive console.

```{r, eval=FALSE}
ctx <- v8()
ctx$console()
```

From JavaScript we can read/write R objects via `console.r.get` and `console.r.assign`. The final argument is an optional list specifying arguments passed to `toJSON` or `fromJSON`.

```javascript
// read the iris object into JS
var iris = console.r.get("iris")
var iris_col = console.r.get("iris", {dataframe : "col"})

//write an object back to the R session
console.r.assign("iris2", iris)
console.r.assign("iris3", iris, {simplifyVector : false})
```

To call R functions use `console.r.call`. The first argument should be a string which evaluates to a function. The second argument contains a list of arguments passed to the function, similar to `do.call` in R. Both named and unnamed lists are supported. The return object is returned to JavaScript via JSON.

```javascript
//calls rnorm(n=2, mean=10, sd=5)
var out = console.r.call('rnorm', {n: 2,mean:10, sd:5})
var out = console.r.call('rnorm', [2, 20, 5])

//anonymous function
var out = console.r.call('function(x){x^2}', {x:12})
```

There is also an `console.r.eval` function, which evaluates some code. It takes only a single argument (the string to evaluate) and does not return anything. Output is printed to the console.

```javascript
console.r.eval('sessionInfo()')
```

238 changes: 136 additions & 102 deletions inst/doc/v8_intro.html

Large diffs are not rendered by default.

0 comments on commit f9f2403

Please sign in to comment.