Search code examples
angularjsrmeteorrestful-architectureopencpu

Opencpu and Meteor


I have seen some examples of using opencpu together with angular, but no examples of using opencpu in meteor (where angular could be inplemented easily).

Is it as easy as just including ocpu.seturl and jquery.min.js in meteor (as is done here), or does one need to think differently in meteor when using opencpu?

For example, there might be some conflicts between angular and meteor.

I know that is a diffuse question, but I've seen that I'm not the only one who does wonder about it.
Related:

For example (thanks to http://jsfiddle.net/ramnathv/uatjd/15/):

var myApp = angular.module('myApp', ['angular-meteor']); //added 'angular-meteor'

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


myApp.factory('OpenCPU', function($http){
  return {
     Dist: function(dist){
       var url = "http://public.opencpu.org//ocpu/library/stats/R/" + dist + 
          "/json"
       return $http.post(url, {n: 100})
     }
  }
})

myApp.controller("HistCtrl", function($scope, $http, OpenCPU){    
    $scope.dist = 'rnorm'
    $scope.dists = ['rnorm', 'runif']
    $scope.color = 'blue'
    $scope.colors = ['blue', 'red', 'darkmagenta']
    $scope.breaks = 10
    $scope.submit = function(){
      var req = $("#plotdiv").rplot("hist", {
         x : $scope.data,
         col: $scope.color,
         breaks: Math.floor($scope.breaks),
         main: $scope.main
      });   
    }
    $scope.$watchCollection('[main, color, breaks, data]', function(x){
     $scope.submit()  
    })
    $scope.$watch('dist', function(newDist){
       OpenCPU.Dist(newDist).success(function(result){
         $scope.data = result  
       }) 
    })
})

Would the above be a "correct" starting point? How should one declare dependencies in meteor (i.e. opencpu, jquery.min.js) ? New to meteor so any suggestions are highly appreciated!


Solution

  • Not using angular (not sure why one would need that), but here is a super basic setup in meteor:

    HTML:

    <head>
      <title>opencpu</title>
      <script src="//cdn.opencpu.org/opencpu-0.4.js"></script>
    </head>
    
    <body>
      <h1>Testing OpenCPU</h1>
      {{> hello}}
    </body>
    
    <template name="hello">
    </template>
    

    JS:

    if (Meteor.isClient) {
    
        Template.hello.onRendered(function() {
            // ocpu.seturl("//public.opencpu.org/ocpu/library/graphics/R");
            // couldn't come up with a good example for this
    
            ocpu.seturl("//public.opencpu.org/ocpu/library/stats/R")
            // this gives me a CORS error but the below still seems to work
            console.log(ocpu);
    
            var req1 = ocpu.call("rnorm", {n: 100}, function(session1){
                var req2 = ocpu.call("var", {x : session1}, function(session2){
                    session2.getObject(function(data){
                        alert("Variance equals: " + data);
                    });
                });
            });
        });
    }
    

    All I know about opencpu I've learned in the last 30 minutes -- little! So I don't know how to get past the CORS error. That error doesn't seem to happen when pointing at the graphics package, but for that one I couldn't think of a good example.