Skip to content

Instantly share code, notes, and snippets.

@timelyportfolio
Last active August 29, 2015 14:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save timelyportfolio/8f8d94d59ab52ebc81b4 to your computer and use it in GitHub Desktop.
Save timelyportfolio/8f8d94d59ab52ebc81b4 to your computer and use it in GitHub Desktop.
R tests Javascript simple-statistics with V8

With V8 from Jeroen Ooms, we can do things like use R totest this Javascript stats library simple-statistics from Tom McWright.

# check simple-statistics with R
# https://github.com/tmcw/simple-statistics
library(V8)
library(jsonlite)
ct = new_context("window")
ct$source("https://raw.githubusercontent.com/tmcw/simple-statistics/master/src/simple_statistics.js")
coef(lm( mpg ~ wt, mtcars ))
ct$eval(
sprintf("
var lm = ss.linear_regression()
.data(%s);
[ lm.b(), lm.m() ]
",toJSON(lapply(1:nrow(mtcars),function(x){c(mtcars$wt[x],mtcars$mpg[x])}))
)
)
@jeroen
Copy link

jeroen commented Feb 16, 2015

Cool! Here is a slightly more cleaned up version: https://gist.github.com/jeroenooms/a5b07a669285da699056

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment