Giter VIP home page Giter VIP logo

renjin-spring-boot-starter's Introduction

renjin-spring-boot-starter

A spring boot starter to run Renjin R code in a spring boot app

To use it, add the following dependency to your pom.xml

<dependency>
    <groupId>se.alipsa</groupId>
    <artifactId>renjin-spring-boot-starter</artifactId>
    <version>1.0.0</version>
</dependency>

Then, in your spring boot application, you can wire in the ScriptEngine to execute R code. E.g. (very simplified)

Import the RenjinStarterAutoConfig in one of your configuration beans:

package my.springapp;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import se.alipsa.renjin.starter.RenjinStarterAutoConfig;

@Configuration
@Import(RenjinStarterAutoConfig.class)
public class BeanConfig {
  // Your other beans goes here...
}

Now you can just autowire a RenjinScriptEngine in your service(s), e.g:

package my.springapp;

import org.renjin.script.RenjinScriptEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.script.ScriptException;
import java.io.IOException;

@Service
public class AnalysisEngine {

  private final RenjinScriptEngine scriptEngine;

  @Autowired
  public AnalysisEngine(RenjinScriptEngine scriptEngine) {
    this.scriptEngine = scriptEngine;
  }
  
  public SEXP runScript(String script) throws ScriptException {
    return (SEXP)scriptEngine.eval(script);
  }
}

Since any executions in the same session share data (e.g. variables in the global environment), in some cases you need to have a new Session for every script invocation. As session creation is a bit expensive, there is an Object pool of Script Engines with a unique session for each that you can use as follows:

import org.renjin.script.RenjinScriptEngine;
import org.renjin.sexp.SEXP;
import org.springframework.beans.factory.annotation.Autowired;;
import org.springframework.stereotype.Service;
import se.alipsa.renjin.starter.RenjinSessionEnginePool;
import java.util.Map;

@Service
public class ReportEngine {
  private final RenjinSessionEnginePool renjinSessionEnginePool;

  @Autowired
  public ReportEngine(RenjinSessionEnginePool renjinSessionEnginePool) {
    this.renjinSessionEnginePool = renjinSessionEnginePool;
  }

  private SEXP runScript(String script, Map<String, Object> params) throws Exception {
    RenjinScriptEngine scriptEngine = null;
    try {
      scriptEngine = renjinSessionEnginePool.borrowObject();
      params.forEach(scriptEngine::put);
      return (SEXP) scriptEngine.eval(script);
    } finally {
      if (scriptEngine != null) {
        renjinSessionEnginePool.returnObject(scriptEngine);
      }
    }
  }
}

Note: When the engine is returned to the pool, all top level variables are removed with rm(list = ls(all.names = TRUE)) so that the next script will start with a clean environment.

For more comprehensive examples see https://github.com/perNyfelt/renjin-spring-boot-starter-examples

Configuration

Default value in bold

  • se.alipsa.renjin.starter.excludeDefaultPackages=true / false
  • se.alipsa.renjin.starter.packageLoader=AetherPackageLoader / ClasspathPackageLoader

Release History

v1.0.1 in progress

  • Set compile version to java 11 (was 1.8)
  • Upgrade dependencies for commons pool, slf4j, junit
  • Add spotbugs annotations to exclude some false positives

v 1.0.0, 2020-12-20

renjin-spring-boot-starter's People

Contributors

pernyfelt avatar

Stargazers

 avatar

Watchers

 avatar

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.