I tried to use t-test in R using OpenCPU as follows -
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="//cdn.opencpu.org/opencpu-0.4.js"></script>
and
ocpu.seturl("//public.opencpu.org/ocpu/library/stats/R")
var x = [1,2,3,4,5,6,7,8,9,10];
var y = [7,8,9,10,11,12,13,14,15,16,17,18,19,20];
// call R function: stats::t.test
var req = ocpu.rpc("t.test",{
"x" : x,
"y" : y
}, function(output){
alert("t.test equals: " + output);
});
//optional
req.fail(function(){
alert("R returned an error: " + req.responseText);
});
But I got this error
I am unable to understand where I am going wrong
The ocpu.rpc
function is a shorthand that retrieves the output as JSON. However there is no JSON representation of a t.test object. Therefore you can use ocpu.call
and retrieve e.g. the console output from the session [fiddle]:
var req = ocpu.call("t.test",{
x : x,
y : y
}, function(session){
session.getConsole(function(outtxt){
$("code").text(outtxt);
});
});
If you want actual data (e.g p-value), I recommend you create an simple R package with a wrapper function that returns a list with data you are interested in:
my_ttest <- function(x, y){
out <- t.test(x,y)
list(
n1 = length(x),
n2 = length(y),
p = out$p.value
)
}
You will be able to call this function using ocpu.rpc
as you did above, because the list can be mapped directly to JSON. Note that you can easily deploy your own package on the public demo server with the github webhook.