Git Product home page Git Product logo

rconnector's Introduction

rconnector

Easy to use API to connect and execute R scripts from Java. Also handles the timeouts well.

Usage: Add dist/rconnector as a dependency in project

Define spring bean or initialize it as a stand alone class

<bean class="com.leantaas.rconnector.RConnectionHandler" id="rConnectionHandler"> 
    <property name="rServeHost" value="${r.serve.host:localhost}"/>
    <property name="rServePort" value="${r.serve.port:6311}"/> 
    <property name="rScriptHomeDir" value="{r.scripts.home.directory:/opt/Rscripts/rProject/}"/>
    <property name="timeoutInSec" value="${r.script.timeout:900}"></property>
</bean>

Inject in your Service You could either Autowire it or provide a setter method (or create a new instance)

@Autowired private RConnectionHandler rConnectionHandler;

Ready to use

//create input with input and output variables 
RInput rIn = new RInput("opt_all.r"); 
rIn.getInputVarables().put("model_data", modelData); 
rIn.getOutputVars().add("best_op"); 
rIn.getOutputVars().add("RELAXED"); 
rIn.getOutputVars().add("json_opt"); 
try {
    //call R       
    ROutput rOut = rConnectionHandler.executeScript(rIn);
    //read output       
    String[][] output = rOut.get("best_op").asStringMatrix();
    Boolean relaxed = rOut.get("RELAXED").asBasic(Boolean.class);
    String jsonOpt = rOut.get("json_opt").asString();
    //process output 
} catch (RTimedoutException rte) {
    // this is a RuntimeException, catch if you want to do something with timed out requests
    logger.warn("Request Timedout for: {}", rIn);
}

Note: Avoid using "setwd" in R scripts because Rserve start its own R session and working directory. "rconnector" paasses the "base_dir" with rScriptHomeDir as a value, we can make use of it for relative paths.

Coming soon:

YourPojo obj = rOut.get(“json_out”).asObject(YourPojo.class);

Bitdeli Badge

rconnector's People

Contributors

rajendrag avatar bitdeli-chef avatar

Stargazers

Kiran Danduprolu avatar  avatar

Watchers

James Cloos avatar  avatar

Forkers

iqiuyu-0821

rconnector's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.