A scoring engine for predictive analytics with OpenCPU

by opencpu

HTML

<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<script src="//cdn.opencpu.org/opencpu-0.4.js"></script>
<h1>Scoring from JavaScript</h1>

<!-- some text, not part of the app -->
<p>See this <a href="https://www.opencpu.org/posts/scoring-engine/">this blog post</a> for more information about this app.</p>
<button id="submitbutton">Score data</button> <br>

<table class="table table-hover">
    <thead><tr><th>Age</th><th>Marital</th><th>TV Score</th></tr></thead>
    <tbody></tbody>
</table>

CSS

body{
    margin: 10px;
}

JavaScript

//set CORS to call "tvscore" package on public server
ocpu.seturl("//public.opencpu.org/ocpu/library/tvscore/R")

//some example data
//to run with different data, edit and press Run at the top of the page
var mydata = [
  {age : 43, marital: "MARRIED"}, 
  {age : 54, marital: "DIVORCED"}, 
  {age : 32, marital: "NEVER MARRIED"}    
];

//call R function: tvscore::tv(input=data)
$("#submitbutton").click(function(){
    
  var req = ocpu.rpc("tv",{
    input : mydata
  }, function(output){
    $("tbody").empty();
    $.each(output, function(index, value){
      var html = "<tr><td>" + value.age + "</td><td>" + value.marital + "</td><td>" + value.tv + "</td></tr>";
      $("tbody").append(html);
    });
  });

  //optional
  req.fail(function(){
    alert("R returned an error: " + req.responseText); 
  });
});