1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| library(V8) ctx <- v8() ctx$source("http://underscorejs.org/underscore-min.js") ctx$eval("var list = [[0, 1], [2, 3], [4, 5]];") ctx$eval("var flat = _.reduceRight(list, function(a, b) { return a.concat(b); }, []);") ctx$get("flat")
ctx$assign("x", 1:5) ctx$eval("var y = _.each(x, function(x){ return x*3;})") ctx$get("y")
x2 <- 1:5 ctx$call("_.each", x2, JS("function(x){ return x*3;}"))
ctx$call("_.filter", mtcars, JS("function(x){return x.mpg < 15}"))
Sys.setenv(PATH = paste0(Sys.getenv("PATH"), ";C:\\Program Files\\nodejs;C:\\Users\\Jamal\\AppData\\Roaming\\npm")) Sys.setenv(NODE_PATH = "C:\\Program Files\\nodejs\\node_modules;C:\\Users\\Jamal\\AppData\\Roaming\\npm\\node_modules") system("npm install -g browserify js-beautify") writeLines("global.beautify = require('js-beautify');", "in.js") system("browserify in.js -o bundle.js")
ct <- v8() ct$source("bundle.js") test <- "(function(x,y){x = x || 1; y = y || 1; return y * x;})(4, 9)" pretty_test <- ct$call("pr.js_beautify", test, list(indent_size = 2)) cat(pretty_test)
|