opencpu

前端通过它调用后端的R语言,对R函数进行一个封装。

网址:https://github.com/jeroenooms/opencpu.js

 

使用的是opencpu-0.5.js,对它进行了修改。

1、先调用call方法,在它的回调中,调用rpc和rplot。之前是通过按钮触发rpc的调用,所以没有写在call的回调中,也不会有影响。

2、ocpu.rpc();  $('#sfp').rplot();会把返回的html直接appendTo('#sfp')。 调用方式不同

3、自己的修改:新建了一个rplot,调用方式为ocpu.rplot(); 可以直接返回三个图片的url,之前是返回html代码。

// call a function and return JSON, relative plot
function rplot(fun, args, cb){
  return r_fun_call(fun, args, function(tmp){
    var Location = tmp.getLoc();
    var n = 'last';
    var result = {};
    result.pdf = Location + "graphics/" + n + "/pdf?width=11.69&height=8.27&paper=a4r";
    result.svg = Location + "graphics/" + n + "/svg?width=11&height=6";
    result.png = Location + "graphics/" + n + "/png?width=800&height=600";
    
    if(cb) cb(result);
    return result;      
  });
}

增加:ocpu.rplot = rplot; 

 

call调用R函数

var req = ocpu.call("readcsvnew", {
  file: myfile,
  header: myheader
}, function(session) {
  session1 = session;
  $("#printlink").attr("href", session1.getLoc() + "R/.val/print")
  $("#rdalink").attr("href", session1.getLoc() + "R/.val/rda")
  $("#csvlink").attr("href", session1.getLoc() + "R/.val/csv")
});

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

//after request complete, re-enable the button
req.always(function() {
  $("#submitbutton").removeAttr("disabled")
});

rpc调用R函数:和call的区别,不同点是callback的参数是session,rpc的callback参数是json,调用的R函数都是自己写好的。

var req = ocpu.rpc("demoSummary", {
  data: session1
  }, function(output) {
    $('#summary').html(output)
      //                  alert("Standard Deviation equals: " + output);
  });
//optional
req.fail(function() {
  alert("R returned an error: " + req.responseText);
});

R plot:$('#mydiv').rplot(fun, [, argu] [, callback])

 

posted on 2015-09-27 21:30  j.w  阅读(1370)  评论(0编辑  收藏  举报