Search code examples
rgitgithubrstudioopencpu

Jiddle for executing R function published on OpenCPU


I uploaded my R package to GitHub and then published it on OpenCPU as explained here.

https://public.opencpu.org/ocpu/github/Klausos9/test/R/test/print

test is a function that contains squared root estimation formula.

Now, in JFiddle, I am trying to make a simple Call of this function using HTTP API. However, I cannot make it working. Any idea?

http://jsfiddle.net/WVWCR/49/

But when I click the Run button, it says:

R returned an error: unused argument (input = input)

In call:
test(input = input)

Solution

  • Try changing the ocpu.rpc call to:

    var req = ocpu.rpc("test",{
        x : mydata                        // <--- input : mydata
      }, function(output){
        $("tbody").empty();
        $.each(output, function(index, value){
          var html = "<tr><td>" + value.x + "</td><td>" + value.tv + "</td></tr>";
        $("tbody").append(html);
    });
    

    The error is coming because your function call passes an argument named input while your function is expecting an argument named x.

    EDIT

    The full corrected script (for the one mentioned in comments below) :-

      ocpu.seturl("//public.opencpu.org/ocpu/github/Klausos9/test/R")
    
      //some example data
      //to run with different data, edit and press Run at the top of the 
      //page
      var mydata = 2;
    
      //call R function: tvscore::tv(input=data)
      $("#submitbutton").click(function(){      // <--- needed
          var req = ocpu.rpc("test",{
              x : mydata                        // <--- changed; input : mydata
            }, function(output){
             $("#output").text(output);         // <--- changed; output.message
          });
    
        //optional
        req.fail(function(){
          alert("R returned an error: " + req.responseText); 
        });
      });