Skip to content

Instantly share code, notes, and snippets.

@timelyportfolio
Last active July 9, 2021 18:52
R | Turf.js with V8
#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" )
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment