Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New demo showing FastR and Node.js interop
PullRequest: graalvm-examples/21
- Loading branch information
Showing
17 changed files
with
777 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
weather_predictor/node_modules/ | ||
fastr_node/node_modules/ | ||
weather_predictor/openweather/ | ||
weather_predictor/rlang/ | ||
weather_predictor/bin/ |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
library(lattice) | ||
|
||
plotkmeans <- function(x, y, clusters) { | ||
svg(width=10, height=8) | ||
data <- kmeans(iris[, c(y, x)], clusters) | ||
iris$cluster <- factor(data$cluster) | ||
print(xyplot(as.formula(paste0(y,'~',x)), data=iris, groups=cluster, pch=20, cex=3)) | ||
grDevices:::svg.off() | ||
} | ||
|
||
plotcars <- function(x, y, z) { | ||
svg(width=10, height=8) | ||
mtcars$gear.f<-factor(mtcars$gear,levels=c(3,4,5), labels=c("3gears","4gears","5gears")) | ||
mtcars$cyl.f <-factor(mtcars$cyl,levels=c(4,6,8), labels=c("4cyl","6cyl","8cyl")) | ||
print(cloud(as.formula(paste0(x,'~',y,'*',z)), main="3D Scatterplot generated in R", data=mtcars)) | ||
grDevices:::svg.off() | ||
} | ||
|
||
model <- lm(weight~height, data=women) | ||
|
||
plotheightweitgh <- function(x, y, z) { | ||
svg(width=10, height=8) | ||
print(xyplot(weight ~ height, data = women, | ||
panel = function(x, y) { | ||
panel.xyplot(x, y, cex=2, pch=19) | ||
panel.abline(model) | ||
})); | ||
grDevices:::svg.off() | ||
} | ||
|
||
predictweight <- function(height) { | ||
predict(model, as.data.frame(list(height=height))) | ||
} | ||
|
||
export('plotkmeans', plotkmeans) | ||
export('plotcars', plotcars) | ||
export('plotheightweight', plotheightweitgh) | ||
export('predictweight', predictweight) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "fastr_nodejs", | ||
"version": "1.0.0", | ||
"description": "GraalVM interoperability demo: copy of a Shiny example that scales!", | ||
"main": "server.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "GraalVM team", | ||
"license": "Universal Permissive License (UPL)", | ||
"dependencies": { | ||
"express": "^4.15.2" | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> | ||
<meta name="description" content="GraalVM demo"> | ||
<meta name="author" content="Oracle Labs"> | ||
<title>GraalVM ala Shiny demo</title> | ||
<link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet"> | ||
<link href="style.css" rel="stylesheet"> | ||
</head> | ||
<body> | ||
<div class="row text-center"> | ||
<h1><span style='color:red'>Oracle</span> Labs GraalVM <small>demo</small></h1> | ||
</div> | ||
<nav class="navbar navbar-default" role="navigation"> | ||
<div class="container-fluid" id="navfluid"> | ||
<div class="row"> | ||
<div class="collapse navbar-collapse navbar-inner"> | ||
<ul class="nav navbar-nav "> | ||
<li class="active"> | ||
<a href="#" data-page="kmeans"><span class="glyphicon glyphicon-equalizer"></span> K-means clustering</a> | ||
</li> | ||
<li> | ||
<a href="#" data-page="cars"><span class="glyphicon glyphicon-stats"></span> Cars dataset</a> | ||
</li> | ||
<li> | ||
<a href="#" data-page="lm"><span class="glyphicon glyphicon-scale"></span> Linear model prediction</a> | ||
</li> | ||
</ul> | ||
</div> | ||
</div> | ||
</div> | ||
</nav> | ||
|
||
<div class="container-fluid page" style="padding-top: 2em" id="kmeans"> | ||
<div class="row"> | ||
<div class="form-box col-sm-8 col-sm-offset-2 col-md-offset-1 col-md-2 col-lg-2"> | ||
<form role="form" class="plot-form"> | ||
<div class="form-group"> | ||
<label for="xaxis">X-axis variable</label> | ||
<select id="xaxis" class="input form-control"> | ||
<option value="Sepal.Length">Sepal.Length</option> | ||
<option value="Sepal.Width">Sepal.Width</option> | ||
<option value="Petal.Length">Petal.Length</option> | ||
<option value="Petal.Width">Petal.Width</option> | ||
<select> | ||
</div> | ||
<div class="form-group"> | ||
<label for="yaxis">Y-axis variable</label> | ||
<select id="yaxis" class="input form-control"> | ||
<option value="Sepal.Length">Sepal.Length</option> | ||
<option value="Sepal.Width" selected>Sepal.Width</option> | ||
<option value="Petal.Length">Petal.Length</option> | ||
<option value="Petal.Width">Petal.Width</option> | ||
<select> | ||
</div> | ||
<div class="form-group"> | ||
<label for="clusters">Clusters</label> | ||
<select id="clusters" class="input form-control"> | ||
<option value="3">3</option> | ||
<option value="4" selected>4</option> | ||
<option value="5">5</option> | ||
<option value="6">6</option> | ||
<select> | ||
</div> | ||
</form> | ||
</div> | ||
<div> | ||
<p>Visualization of k-means clustering algorithm. Both the clusters computation | ||
and the visualization are generated by FastR. This sample was inspired by similar demo used by Shiny web application framework for R. | ||
In this case, the web part is implemented in Node.js</p> | ||
<div class="plot"></div> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<div class="container-fluid page" style="padding-top: 2em; display: none" id="cars"> | ||
<div class="row"> | ||
<div class="form-box col-sm-8 col-sm-offset-2 col-md-offset-1 col-md-2 col-lg-2"> | ||
<form role="form" class="plot-form"> | ||
<div class="form-group"> | ||
<label for="xaxis">X-axis variable</label> | ||
<select id="xaxis" class="input form-control"> | ||
<option value="mpg">Miles per galon</option> | ||
<option value="cyl">Cylinders</option> | ||
<option value="disp">Displacement</option> | ||
<option value="hp">Horsepower</option> | ||
<option value="drat">Rear axle ratio</option> | ||
<option value="wt">Weight</option> | ||
<option value="qsec">1/4 mile time</option> | ||
<option value="gear"># of forward gears</option> | ||
<select> | ||
</div> | ||
<div class="form-group"> | ||
<label for="yaxis">Y-axis variable</label> | ||
<select id="yaxis" class="input form-control"> | ||
<option value="mpg">Miles per galon</option> | ||
<option value="cyl" selected>Cylinders</option> | ||
<option value="disp">Displacement</option> | ||
<option value="hp">Horsepower</option> | ||
<option value="drat">Rear axle ratio</option> | ||
<option value="wt">Weight</option> | ||
<option value="qsec">1/4 mile time</option> | ||
<option value="gear"># of forward gears</option> | ||
<select> | ||
</div> | ||
<div class="form-group"> | ||
<label for="zaxis">Z-axis variable</label> | ||
<select id="zaxis" class="input form-control"> | ||
<option value="mpg">Miles per galon</option> | ||
<option value="cyl">Cylinders</option> | ||
<option value="disp" selected>Displacement</option> | ||
<option value="hp">Horsepower</option> | ||
<option value="drat">Rear axle ratio</option> | ||
<option value="wt">Weight</option> | ||
<option value="qsec">1/4 mile time</option> | ||
<option value="gear"># of forward gears</option> | ||
<select> | ||
</div> | ||
</form> | ||
</div> | ||
<div> | ||
<p>This is an example plot with 3 axes. | ||
The data used come from built-in <code>mtcars</code> dataset.</p> | ||
<div class="plot"></div> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<div class="container-fluid page" style="padding-top: 2em; display: none" id="lm"> | ||
<div class="row"> | ||
<div class="form-box col-sm-8 col-sm-offset-2 col-md-offset-1 col-md-2 col-lg-2"> | ||
<h4>Linear Model Prediction</h4> | ||
<form role="form"> | ||
<div class="form-group"> | ||
<label for="height">Height (in)</label> | ||
<input type="number" id="height" class="input form-control"> | ||
</div> | ||
<div class="form-group"> | ||
<div class="col-sm-offset-2 col-sm-10"></div> | ||
<button id="predict-button" type="submit" class="btn btn-default">Predict</button> | ||
</div> | ||
</form> | ||
<hr/> | ||
<p id="prediction-text" style="display:none">Predicted weight is <em id="prediction"></em> lbs</p> | ||
</div> | ||
<div> | ||
<p>This is a classical example of linear regression computed and visualized by FastR | ||
using <code>lm</code> built-in function and <code>lattice</code> package. | ||
The data used come from built-in <code>women</code> dataset | ||
(the average heights and weights for American women aged 30–39).</p> | ||
<div class="plot"></div> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<script src="vendor/jquery.min.js"></script> | ||
<script src="vendor/bootstrap/js/bootstrap.min.js"></script> | ||
<script src="script.js"></script> | ||
</body> | ||
</html> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
$(document).ready(function() { | ||
// navigation between pages: | ||
$('.navbar-nav a').click(function(e) { | ||
$('.page').hide(); | ||
$('.navbar-nav .active').removeClass('active') | ||
$('#' + $(this).data('page')).show(); | ||
$(this).parent().addClass('active') | ||
e.preventDefault(); | ||
return false; | ||
}); | ||
|
||
// data loading | ||
var refresh = function(page) { | ||
var query = {}; | ||
let inputs = page.find('.plot-form .input').each((idx, e) => query[$(e).attr('id')] = $(e).val()); | ||
$.get('http://localhost:12836/' + page.attr('id'), query, function(data) { | ||
page.find('.plot').html(data); | ||
}); | ||
} | ||
|
||
refresh($('#kmeans')); | ||
refresh($('#cars')); | ||
refresh($('#lm')); | ||
|
||
$('.input').change(function(e) { | ||
refresh($(this).parents('.page:first')); | ||
e.preventDefault(); | ||
return false; | ||
}); | ||
|
||
// linear regression | ||
$('#predict-button').click((e) => { | ||
let height = $('#height').val(); | ||
$('#prediction-text').show(2000); | ||
$.get('http://localhost:12836/lm/predict/', {height: height}, function(data) { | ||
$('#prediction').html(data); | ||
}); | ||
e.preventDefault(); | ||
return false; | ||
}); | ||
}); |
Oops, something went wrong.