2

I have a R code, which has a trained machine learning model. Now I want to get prediction for new data.

Current method:

Script code.R '[{"x1" : "1011", "x2" : "1031", "x4" : "0.65"}]'

I would get the answer back, the problem was it took too much time just to load and set up the environment

Code:

# Loading the library

suppressPackageStartupMessages(library(C50))
suppressPackageStartupMessages(library(jsonlite))
suppressPackageStartupMessages(library(plyr))

# Loading the trained model

model1 <- readRDS("model1.RDA")

 x <- (args[1])

# Function which wraps prediction and son handling

output <- function(x) {

df <- fromJSON(x)

df[is.na(df)] <- 0

prediction <- predict.C5.0(model1, newdata = df, type = "class")

json_df <- toJSON(prediction)

return(json_df)
}

 output(x)

Problem:

  • I would like to use Rserve and pass parameters to this, I am not able to figure it out how? What modification should I do?

  • I know to add library(Rserve) and then do run.Rserve() but beyond that I don't know how?

Anubhav Dikshit
  • 1,729
  • 6
  • 25
  • 48
  • Your code seems to be incomplete (missing closing bracket of output function and no call of the output function, can you please edit your question. Thx :-) – R Yoda Jan 11 '17 at 07:37
  • @RYoda Added it, thanks for this! – Anubhav Dikshit Jan 11 '17 at 07:48
  • I think you basically have to split your code into client and server side scripts. Servers side: Remove the `x` assignment and `output` function call rows from your sorce code and deploy this script with `model1.RDA` into a folder on the server. Client side: set x variable, RSconnect, RSserversource, RSassign x, RSeval... (see the doc of `RSclient` package: https://cran.r-project.org/web/packages/RSclient/RSclient.pdf for details) – R Yoda Jan 11 '17 at 07:55
  • @RYoda Thanks for this, will check it out – Anubhav Dikshit Jan 11 '17 at 07:57
  • @RYoda: Can you just tell me how to query Rserve using a browser or terminal, a very simple example like 2+2 will also do, I trying "curl localhost:6311 2+2" but i get nothing. Rserve is running on 6311! – Anubhav Dikshit Jan 11 '17 at 08:28
  • `# call this R script file using "Rscript myscript.R 'params'" library(RSclient) c <- RS.connect(port = 6311) RS.eval(c, 2 + 2) RS.close(c)` – R Yoda Jan 11 '17 at 09:13
  • Thanks a lot for this, so is there no way for me to do a POST or GET to Rserve? even here i am opening an R shell, this is expensive – Anubhav Dikshit Jan 11 '17 at 09:17
  • 1
    Rserve is TCP/IP based, not HTTP (AFAIK) so you need a client-side library (sorry, I have no experiences with Rserve w/o R). Please modify your question as it looks like you want to use an R client. – R Yoda Jan 11 '17 at 11:01
  • Actually your answer helped, I know my approach now (using node to talk to Rserve) – Anubhav Dikshit Jan 12 '17 at 09:06
  • Great! Please post the solution as answer here to let us all learn from your experiences. – R Yoda Jan 12 '17 at 18:12

2 Answers2

1

So i am using NodeJS to talk to Rserve.

First install node and npm from terminal.

npm install --save rserve-client

The code in the javascript file is:

var r = require('rserve-client');
r.connect('localhost', 6311, function(err, client) {
    client.evaluate('a<-2.7+2', function(err, ans) {
        console.log(ans);
        client.end();
    });
});
  • Observe that the node is talking on port '6311' (default for Rserve)

  • the part 'a<-2.7+2' is what is send to R, modify this part

Anubhav Dikshit
  • 1,729
  • 6
  • 25
  • 48
  • Thanks for posting the answer. It's absolutly OK th mark your answer as accepted! Some more links: See [what is npm](https://docs.npmjs.com/getting-started/what-is-npm) and [npm package rserve-client](https://www.npmjs.com/package/rserve-client) – R Yoda Jan 13 '17 at 17:29
1

If you want to use Java client, you need to download the REngine jars and the following code will help (which I am using from Java):

import org.rosuda.REngine.*;
import org.rosuda.REngine.Rserve.*;
import java.util.Arrays;

public class RServeCommunication {

    public static void main(String[] args) {
      try {
        // Setup the connection with RServer through RServe
        RConnection rConnection = new RConnection("127.0.0.1", 6311); 
        // Call R function 
        REXP retValues = rConnection.eval("rnorm(10)");
        // print the values returned to console
        System.out.println(Arrays.toString(retValues.asDoubles()));
        // [1.4280800442888217, -0.11989685521338722, 0.6836180891125788, 0.7913069852336285, -0.8968664915403061, 0.0681405000990237, -0.08726481416189025, -0.5322959519563994, -0.3302662744787598, 0.45030516965234024]

      }
      catch (REngineException e) {
        System.out.println("Error: " + e.toString());
      }
      catch (REXPMismatchException e) {
        System.out.println("Error: " + e.toString());
      }
    } 
}
Sandipan Dey
  • 21,482
  • 2
  • 51
  • 63