Skip to content

Instantly share code, notes, and snippets.

@jeroen
Forked from timelyportfolio/code.R
Last active August 29, 2015 14:15
Show Gist options
  • Save jeroen/6df0f822ea839165347d to your computer and use it in GitHub Desktop.
Save jeroen/6df0f822ea839165347d to your computer and use it in GitHub Desktop.
V8 example with turf.js
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
#devtools::install("jeroenooms/V8")
library("V8")
library(pipeR)
ct = new_context("window")
# min.js gives me a call stack size error but non-min works fine
ct$source(
"https://raw.githubusercontent.com/morganherlocker/turf/master/turf.js"
)
# one of the examples from turf API docs
# http://turfjs.org/static/docs/module-turf_simplify.html
ct$eval('
var feature = turf.polygon([[
[-70.603637, -33.399918],
[-70.614624, -33.395332],
[-70.639343, -33.392466],
[-70.659942, -33.394759],
[-70.683975, -33.404504],
[-70.697021, -33.419406],
[-70.701141, -33.434306],
[-70.700454, -33.446339],
[-70.694274, -33.458369],
[-70.682601, -33.465816],
[-70.668869, -33.472117],
[-70.646209, -33.473835],
[-70.624923, -33.472117],
[-70.609817, -33.468107],
[-70.595397, -33.458369],
[-70.587158, -33.442901],
[-70.587158, -33.426283],
[-70.590591, -33.414248],
[-70.594711, -33.406224],
[-70.603637, -33.399918]
]]);
var tolerance = 0.01;
var simplified = turf.simplify(
feature, tolerance, false);
')
ct$get("feature") %>>%
( .$geometry$coordinates ) %>>%
( plot( .[,,1], .[,,2], type = "l" ) )
ct$get("simplified") %>>%
( .$geometry$coordinates ) %>>%
( polygon( .[,,1], .[,,2], col = RColorBrewer::brewer.pal(4,"Greys")[3], lty = 3 ) )
title( main = "Turf.js in R with V8" )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment