Created
May 2, 2017 03:58
-
-
Save ramnathv/71b2e5856b1d76234be9bd41a3126066 to your computer and use it in GitHub Desktop.
Using Concaveman in R using V8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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