如何使用ocpu.r_fun_call?

时间:2016-08-21 11:58:27

标签: r opencpu

我可以使用opencpu函数ocpu.rpc。但不是函数ocpu_r_fun_call。为什么上面的代码不起作用?

<html>
  <head>

    <script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
    <script src="//cdn.opencpu.org/opencpu-0.4.js"></script>

    <script>

    $(document).ready(function(){

        $("#submitbutton").click(function(){
          var req = ocpu.r_fun_call("mean", {"x" : 10}, function(session){
            $("#namefield").val(session);
          });
        });

    });

    </script>

  </head>

  <body>

    <input type="text" id="namefield" value="">

    <button id="submitbutton" type="button">Submit!</button>

  </body>
</html>

我也尝试过:

$("#submitbutton").click(function(){
  var req = ocpu.r_fun_call("mean", {"x" : 10}, function(session){
    session.getObject(function(data){
      $("#namefield").val(data);
    });
  });
});

我还尝试了session.getConsole,还尝试了:

  var req = ocpu.r_fun_call("mean", {"x" : 10}, function(session){
$("#submitbutton").on("click", function(){
    $("#namefield").val(session.getObject());
  });
});

1 个答案:

答案 0 :(得分:0)

主要问题是:

  • js函数为ocpu.call,而不是ocpu.r_fun_call

  • mean函数不属于我的包。如果我在我的包中加了average函数:

    average&lt; - mean

然后我可以像opencpu一样使用它:

<html>
  <head>
    <script src="opencpu/jquery-1.10.2.min.js"></script>
    <script src="opencpu/opencpu-0.4.js"></script>

    <script>

    $(document).ready(function(){
      $("#submitbutton").on("click", function(){

        var req = ocpu.call("average", {
          x : 10
        }, function(session){
          session.getObject(function(data){
                $("#namefield").val(data)
              });
        });

        //if R returns an error, alert the error message
        req.fail(function(){
          alert("Server error: " + req.responseText);
        });

      });
    });
    </script>
  </head>

  <body>

    <button id="submitbutton" type="button">Submit!</button>
    <input type="text" id="namefield" value="">

  </body>
</html>

请注意提示,以便在出现问题时了解更多信息:

    //if R returns an error, alert the error message
    req.fail(function(){
      alert("Server error: " + req.responseText);
    });