Skip to content

Instantly share code, notes, and snippets.

@ramnathv
Created May 2, 2017 03:58
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ramnathv/71b2e5856b1d76234be9bd41a3126066 to your computer and use it in GitHub Desktop.
Save ramnathv/71b2e5856b1d76234be9bd41a3126066 to your computer and use it in GitHub Desktop.
Using Concaveman in R using V8
# Using concaveman in R using V8
concaveman <- function(d){
library(V8)
ctx <- v8()
ctx$source('https://www.mapbox.com/bites/00222/concaveman-bundle.js')
jscode <- sprintf(
"var points = %s;var polygon = concaveman(points);",
jsonlite::toJSON(d, dataframe = 'values')
)
ctx$eval(jscode)
setNames(as.data.frame(ctx$get('polygon')), names(d))
}
# Example
d <- data.frame(
x = runif(100),
y = runif(100)
)
library(ggplot2)
ggplot(d, aes(x = x, y = y)) +
geom_point() +
geom_path(data = concaveman(d))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment