Renjin Spring Boot STarter

Spring Boot Starter for integrating Renjin with your Spring Boot Application

License

License

Categories

Categories

Spring Boot Container Microservices
GroupId

GroupId

se.alipsa
ArtifactId

ArtifactId

renjin-spring-boot-starter
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

Renjin Spring Boot STarter
Spring Boot Starter for integrating Renjin with your Spring Boot Application
Project URL

Project URL

https://github.com/perNyfelt/renjin-spring-boot-starter
Source Code Management

Source Code Management

https://github.com/perNyfelt/renjin-spring-boot-starter/tree/main

Download renjin-spring-boot-starter

How to add to project

<!-- https://jarcasting.com/artifacts/se.alipsa/renjin-spring-boot-starter/ -->
<dependency>
    <groupId>se.alipsa</groupId>
    <artifactId>renjin-spring-boot-starter</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/se.alipsa/renjin-spring-boot-starter/
implementation 'se.alipsa:renjin-spring-boot-starter:1.0.0'
// https://jarcasting.com/artifacts/se.alipsa/renjin-spring-boot-starter/
implementation ("se.alipsa:renjin-spring-boot-starter:1.0.0")
'se.alipsa:renjin-spring-boot-starter:jar:1.0.0'
<dependency org="se.alipsa" name="renjin-spring-boot-starter" rev="1.0.0">
  <artifact name="renjin-spring-boot-starter" type="jar" />
</dependency>
@Grapes(
@Grab(group='se.alipsa', module='renjin-spring-boot-starter', version='1.0.0')
)
libraryDependencies += "se.alipsa" % "renjin-spring-boot-starter" % "1.0.0"
[se.alipsa/renjin-spring-boot-starter "1.0.0"]

Dependencies

compile (5)

Group / Artifact Type Version
org.springframework.boot : spring-boot-configuration-processor Optional jar [2.3.4.RELEASE,)
org.renjin » renjin-script-engine jar [0.9.2726,)
org.renjin » renjin-aether-package-loader jar [0.9.2726,)
org.slf4j : slf4j-api jar 1.7.30
org.apache.commons : commons-pool2 jar 2.9.0

provided (1)

Group / Artifact Type Version
org.springframework.boot : spring-boot-starter jar [2.3.4.RELEASE,)

test (2)

Group / Artifact Type Version
org.springframework.boot : spring-boot-starter-test jar [2.3.4.RELEASE,)
junit : junit jar 4.13.1

Project Modules

There are no modules declared in this project.

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)

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

Versions

Version
1.0.0