-2

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!

user1665355
  • 3,324
  • 8
  • 44
  • 84
  • 2
    Nice bounty, but please format the question to be more readable and add related code to it. – easwee Nov 11 '15 at 09:55
  • 2
    I've edited your post to make it more readable, I'll leave it up to you to come up with a question title that better represents your question – Tim Nov 11 '15 at 10:02
  • Thanks guys, I added the example I am thinking about. – user1665355 Nov 11 '15 at 10:23
  • 2
    When you ask if it would be a correct starting point, what do you mean? Does the code work? Does the code achieve all the functionalities you want? If not, then what is the exact issue? I have no knowledge of OpenCPU but I know that jQuery is included client-side on Meteor, and can be used in packages with `api.use('jquery')`. You may want to make your own OpenCPU Meteor package to use it. – Kyll Nov 11 '15 at 10:34
  • @Kyll Basically I am looking for someone who might have a working example of `opencpu + meteor`. Thanks – user1665355 Nov 11 '15 at 10:41
  • 1
    As a working example of anything is an external resource, it is off-topic for StackOverflow as per the [help/on-topic]. Instead, try to integrate yourself these technologies and ask about a specific issue. – Kyll Nov 11 '15 at 10:44
  • Maybe someone do have a working example? – user1665355 Nov 15 '15 at 21:48

1 Answers1

3

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.

Christian Fritz
  • 20,641
  • 3
  • 42
  • 71